I need to be able to use the counter and the array in the same loop. The way it is now I get a java.util.NoSuchElementException (no line found). How can i change my code so that it is no longer trying to go past the end of the file.
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); } //variables String[]fractions = new String[100]; //will take in the fractions String[]split = new String[2]; //used to split the fractions int [] numerator = new int [100]; // store numerators int [] denominator = new int [100]; //store denominators int count = 0; //number of lines in file while(inputFile.hasNextLine()){ for(int i=0; i<=count ; i++){ fractions[i]=inputFile.nextLine(); count++; System.out.println(fractions[i]); } }