Advertisement
s_m4rt

Untitled

Nov 18th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. package zpliku;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.PrintStream;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11. public static void main(String[] args) throws FileNotFoundException {
  12. zapisz();
  13. parzyste();
  14. tylesamo();
  15. czesctrzecia();
  16. cztery();
  17. }
  18. public static void parzyste() {
  19. int count=0;
  20.  
  21. try {
  22. Scanner input = new Scanner(System.in);
  23. File file = new File("plik.txt");
  24.  
  25. input = new Scanner(file);
  26.  
  27. while (input.hasNextLine()) {
  28. String line = input.nextLine();
  29. //System.out.println(line);
  30. if(line.length()%2==0) count++;
  31.  
  32. }
  33. System.out.println("Napisow o parzystej dlugosci jest: " + count);
  34. input.close();
  35.  
  36. } catch (Exception ex) {
  37. ex.printStackTrace();
  38. }
  39. }
  40. public static void tylesamo(){
  41.  
  42. int c = 0;
  43. int t=0;
  44. int j=0;
  45. try {
  46. Scanner input = new Scanner(System.in);
  47. File file = new File("plik.txt");
  48.  
  49. input = new Scanner(file);
  50.  
  51. while (input.hasNextLine()) {
  52. String line = input.nextLine();
  53. //System.out.println(line);
  54. for(int i=0; i<line.length(); i++){
  55. if (line.charAt(i) == '0') c++;
  56. if (line.charAt(i) == '1') j++;
  57.  
  58. }
  59. if(c==j) t++;
  60. c=0; j=0;
  61. }
  62.  
  63. System.out.println("Tyle samo zer i jedynek jest: "+ t);
  64.  
  65. input.close();
  66.  
  67. } catch (Exception ex) {
  68. ex.printStackTrace();
  69. }
  70. }
  71. public static void czesctrzecia(){
  72. int jedynki=0;
  73. int zera=0;
  74. int onlyzero=0;
  75. int onlyone=0;
  76. try {
  77. Scanner input = new Scanner(System.in);
  78. File file = new File("plik.txt");
  79.  
  80. input = new Scanner(file);
  81.  
  82. while (input.hasNextLine()) {
  83. String line = input.nextLine();
  84. for (int i=0; i<line.length(); i++){
  85. if(line.charAt(i) == '0') zera++;
  86. if(line.charAt(i) == '1') jedynki++;
  87. }
  88. if(zera == line.length()) onlyzero++;
  89. if(jedynki == line.length()) onlyone++;
  90. zera=0; jedynki=0;
  91. }
  92.  
  93. System.out.println("Zer jest: " + onlyzero);
  94. System.out.println("Jedynek jest: "+onlyone);
  95.  
  96. input.close();
  97.  
  98. } catch (Exception ex) {
  99. ex.printStackTrace();
  100. }
  101. }
  102. public static void cztery(){
  103. int[] tablica=new int[15];
  104. try {
  105. Scanner input = new Scanner(System.in);
  106. File file = new File("plik.txt");
  107.  
  108. input = new Scanner(file);
  109.  
  110. while (input.hasNextLine()) {
  111. String line = input.nextLine();
  112. if(line.length()==2) tablica[0]++;
  113. if(line.length()==3) tablica[1]++;
  114. if(line.length()==4) tablica[2]++;
  115. if(line.length()==5) tablica[3]++;
  116. if(line.length()==6) tablica[4]++;
  117. if(line.length()==7) tablica[5]++;
  118. if(line.length()==8) tablica[6]++;
  119. if(line.length()==9) tablica[7]++;
  120. if(line.length()==10) tablica[8]++;
  121. if(line.length()==11) tablica[9]++;
  122. if(line.length()==12) tablica[10]++;
  123. if(line.length()==13) tablica[11]++;
  124. if(line.length()==14) tablica[12]++;
  125. if(line.length()==15) tablica[13]++;
  126. if(line.length()==16) tablica[14]++;
  127. }
  128. int j=2;
  129. for (int i=0; i<tablica.length; i++){
  130. System.out.println(j+" znaki: " + tablica[i]);
  131. j++; }
  132.  
  133. input.close();
  134.  
  135. } catch (Exception ex) {
  136. ex.printStackTrace();
  137. }
  138. }
  139. public static void zapisz() throws FileNotFoundException {
  140.  
  141. File file = new File("rozw.txt");
  142. FileOutputStream fos = new FileOutputStream(file);
  143. PrintStream ps = new PrintStream(fos);
  144. System.setOut(ps);
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement