JetBrains Academy: Reading files - Kamil-Jankowski/Learning-JAVA GitHub Wiki

JetBrains Academy: Reading files

Find the greatest number in a file:

There is a file containing a sequence of integers separated by spaces.

Download it and write a Java program that finds the greatest number in this file.

import java.util.Scanner;
import java.io.*;

public class FindMax {
    public static void main (String[] args){
	long max = 0;
	String pathToFiles = "(...)\\dataset_91007.txt";
	File plik = new File(pathToFiles);

	try (Scanner scanner = new Scanner(plik)){
	    while (scanner.hasNext()){
	        long temp = scanner.nextLong();
		max = temp > max ? temp : max;
	    }
	} catch (FileNotFoundException e){
	    System.out.println("File not found");
	}
	System.out.println(max);
    }
}

Count numbers in a file:

There is a file containing a sequence of integers separated by spaces.

Download it and write a Java program and count numbers which are greater than or equal to 9999.

import java.util.*;
import java.io.*;

public class FileReadingCounter{
    public static void main(String[] args){
	int counter = 0;
	String pathToFile = "(...)\\dataset_91022.txt";
	File file = new File(pathToFile);
		
	try (Scanner scanner = new Scanner(file)){
	    while (scanner.hasNext()){
		counter += scanner.nextInt() >= 9999 ? 1 : 0;
	    }
	} catch (FileNotFoundException e){
	    System.out.println(e.getMessage());
	}
	System.out.println(counter);
    }
}

Count even numbers:

There is a file containing a sequence of integers. Each number starts with a new line.

Download the file and write a Java program to count the number of even numbers in this file. You should stop counting if you get 0 or the last number is reached.

import java.util.*;
import java.io.*;

public class CountEven {
    public static void main(String[] args){
        int evenCounter = 0;
        String pathToFile = "(...)\\dataset_91065.txt";
        File file = new File(pathToFile);

        try (Scanner scanner = new Scanner(file)){
            int temp = scanner.nextInt();
            while (scanner.hasNext() && temp != 0){
                evenCounter += temp % 2 == 0 ? 1 : 0;
                temp = scanner.nextInt();
            }
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
        System.out.println(evenCounter);
    }
}

The sum of numbers in a file:

There is a file containing a sequence of integers. Each number starts with a new line.

Download it and write a Java program to find the sum of these numbers.

import java.util.*;
import java.io.*;

public class Sumator{
    public static void main(String[] args){
	long sum = 0;
        String pathToFile = "(...)\\dataset_91033.txt";
	File file = new File(pathToFile);
		
	try (Scanner scanner = new Scanner(file)){
	    while (scanner.hasNext()){
		sum += scanner.nextInt();
	    }
	} catch (FileNotFoundException e){
	    System.out.println(e.getMessage());
	}
	System.out.println(sum);
    }
}

World population:

There is a file which stores world population since 1950, according to United States Census Bureau (2017).

Download the file and write a Java program to find the year in which the largest increase in population occurred in relation to the previous year.

The downloaded file has two columns: year and population.

import java.util.*;
import java.io.*;

public class WorldPopulation{
    public static void main(String[] args){
	String year = "";
	long diffMax = 0;
	long temp = 0;
	String pathToFile = "(...)\\dataset_91069.txt";
	File file = new File(pathToFile);
		
	try (Scanner scanner = new Scanner(file)){
	    String input = "";
	    while (scanner.hasNext()){
		input += scanner.next().replace(",", "") + " ";
	    }
	    String[] inputArray = input.split(" ");
			
	    for(int i = 5; i < inputArray.length; i += 2){
		temp = Long.parseLong(inputArray[i]) - Long.parseLong(inputArray[i-2]);
		if (temp > diffMax){
	   	    diffMax = temp;
		    year = inputArray[i-1];
		}
	    }
	} catch (Exception e){
	    e.printStackTrace();
	}
	System.out.println("Maximum growth in: " + year);
    }
}