Advertisement
s_m4rt

Untitled

Nov 18th, 2015
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 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. File file = new File("plik.txt");
  23. Scanner input = new Scanner(file);
  24.  
  25. while (input.hasNextLine()) {
  26. String line = input.nextLine();
  27. //System.out.println(line);
  28. if(line.length()%2==0) count++;
  29.  
  30. }
  31. System.out.println("Napisow o parzystej dlugosci jest: " + count);
  32. input.close();
  33.  
  34. } catch (Exception ex) {
  35. ex.printStackTrace();
  36. }
  37. }
  38. public static void tylesamo(){
  39.  
  40. int c = 0;
  41. int t=0;
  42. int j=0;
  43. try {
  44. File file = new File("plik.txt");
  45. Scanner input = new Scanner(file);
  46.  
  47. while (input.hasNextLine()) {
  48. String line = input.nextLine();
  49. //System.out.println(line);
  50. for(int i=0; i<line.length(); i++){
  51. if (line.charAt(i) == '0') c++;
  52. if (line.charAt(i) == '1') j++;
  53.  
  54. }
  55. if(c==j) t++;
  56. c=0; j=0;
  57. }
  58.  
  59. System.out.println("Tyle samo zer i jedynek jest: "+ t);
  60.  
  61. input.close();
  62.  
  63. } catch (Exception ex) {
  64. ex.printStackTrace();
  65. }
  66. }
  67. public static void czesctrzecia(){
  68. int jedynki=0;
  69. int zera=0;
  70. int onlyzero=0;
  71. int onlyone=0;
  72. try {
  73. File file = new File("plik.txt");
  74. Scanner input = new Scanner(file);
  75.  
  76. while (input.hasNextLine()) {
  77. String line = input.nextLine();
  78. for (int i=0; i<line.length(); i++){
  79. if(line.charAt(i) == '0') zera++;
  80. if(line.charAt(i) == '1') jedynki++;
  81. }
  82. if(zera == line.length()) onlyzero++;
  83. if(jedynki == line.length()) onlyone++;
  84. zera=0; jedynki=0;
  85. }
  86.  
  87. System.out.println("Zer jest: " + onlyzero);
  88. System.out.println("Jedynek jest: "+onlyone);
  89.  
  90. input.close();
  91.  
  92. } catch (Exception ex) {
  93. ex.printStackTrace();
  94. }
  95. }
  96. public static void cztery(){
  97. int[] tablica=new int[15];
  98. try {
  99. File file = new File("plik.txt");
  100. Scanner input = new Scanner(file);
  101.  
  102. while (input.hasNextLine()) {
  103. String line = input.nextLine();
  104. tablica[line.length() - 2]++;
  105. }
  106. int j=2;
  107. for (int i=0; i<tablica.length; i++){
  108. System.out.println(j+" znaki: " + tablica[i]);
  109. j++; }
  110.  
  111. input.close();
  112.  
  113. } catch (Exception ex) {
  114. ex.printStackTrace();
  115. }
  116. }
  117. public static void zapisz() throws FileNotFoundException {
  118.  
  119. File file = new File("rozw.txt");
  120. FileOutputStream fos = new FileOutputStream(file);
  121. PrintStream ps = new PrintStream(fos);
  122. System.setOut(ps);
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement