So my end goal is to have read a file containing fractions, and be able to count the number of times a specific fraction appears.
Example file:
6/3
7/2
6/3
5/2
So far I have been able to get the program to count the the number of lines. My idea was to us a for loop to take in a line of the file at a time, and put the numbers from the fraction into an array. My problem is that no matter how many different ways i try this it wont work. It keeps outputting null.
import java.util.Scanner; import java.io.FileNotFoundException; import java.lang.String; import java.io.File; import java.io.FileInputStream; public class Assignment1{ public static void main(String [] args){ //Reads file containing fractions Scanner inputFile = null; try { inputFile = new Scanner(new FileInputStream("fractions.txt")); } catch (FileNotFoundException e) { System.out.println("File not found or not opened."); System.exit(0); } fractionCounter(inputFile); } public static void fractionCounter(Scanner file){ //variables String line; // place holder for line String[]test = new String[100]; // array that holds fractions int count= 0; // use to count the number of lines //Count the number of lines in the file while(file.hasNextLine()){ count++; file.nextLine(); for (int i=0; i<=count; i++){ line = file.nextLine(); test = line.split("/"); } file.nextLine(); } line = file.next(); System.out.println(line); //for(int i=0; i<=count; i++){ // line = file.nextLine(); // test = line.split("/"); //} //for(int i = 0; i <= count*2; i++){ // System.out.println(test[i]); //} } }