Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class Zadanie
- {
- public static int moreZerosThanOnes = 0;
- public static int divisibleBy2 = 0;
- public static int divisibleBy8 = 0;
- public static String theGreatestNumber = "0";
- public static String theSmallestNumber = "100000000000000000000000";
- public static int rowOfTheGreatestNumber = 0;
- public static int rowOfTheSmallestNumber = 0;
- public static void main(String[] args) throws IOException
- {
- FileReader file = new FileReader("path\to\the\text\file");
- BufferedReader reader = new BufferedReader(file);
- String line = reader.readLine();
- int row = 1;
- while (line != null)
- {
- hasMoreZerosThanOnes(line);
- isDivisibleBy2or8(line);
- findExtremeNumbers(line, row);
- line = reader.readLine();
- row++;
- }
- reader.close();
- System.out.println("Liczb gdzie więcej jest zer niz jedynek: " + moreZerosThanOnes);
- System.out.println("Liczb podzielnych przez 2: " + divisibleBy2);
- System.out.println("Liczb podzielnych przez 8: " + divisibleBy8);
- System.out.println("Numer wiersza z największą liczbą: " + rowOfTheGreatestNumber);
- System.out.println("Numer wiersza z najmniejszą liczbą: " + rowOfTheSmallestNumber);
- }
- public static void hasMoreZerosThanOnes(String line)
- {
- int replaced = line.replace("1", "").length();
- if (2 * replaced > line.length()) moreZerosThanOnes++;
- }
- public static void isDivisibleBy2or8(String line)
- {
- if (line.endsWith("0")) divisibleBy2++;
- if (line.endsWith("000")) divisibleBy8++;
- }
- public static void findExtremeNumbers(String line, int row)
- {
- if (line.length() > theGreatestNumber.length())
- {
- theGreatestNumber = line;
- rowOfTheGreatestNumber = row;
- }
- else if (line.length() == theGreatestNumber.length())
- {
- if (line.compareTo(theGreatestNumber) == 1)
- {
- theGreatestNumber = line;
- rowOfTheGreatestNumber = row;
- }
- }
- if (line.length() < theSmallestNumber.length())
- {
- theSmallestNumber = line;
- rowOfTheSmallestNumber = row;
- }
- else if (line.length() == theSmallestNumber.length())
- {
- if (line.compareTo(theSmallestNumber) == -1)
- {
- theSmallestNumber = line;
- rowOfTheSmallestNumber = row;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment