0

I'm trying to read a file, and put that information into an array as an object "Train" however I keep getting an InputMismatchException

The file is structured like this:

650
10
Train
23 40
12 09
Train2
02 24
12 15

(and so on).

How do I properly read the file to put the info in the array?

public void fillArray(String info){

    try{
        File f = new File(info);
        Scanner console = new Scanner(f);
        distance = console.nextInt();
        int totalTrains = console.nextInt();
        //console.nextLine();
        for(int i = 0;i<totalTrains;i++){
            String trainName = console.nextLine();
            Time arrival =  new Time(console.nextInt(),console.nextInt());
            Time departure = new Time(console.nextInt(),console.nextInt());
            trains[i]= new Train(trainName, departure, arrival, distance);
            console.nextLine();
        }
    }catch (FileNotFoundException e){
        System.out.println("Cannot locate File");
    }


}

Here is the exact error message I receive when I try to access the distance that I tried to retrieve from the file

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
  • What is the complete error message? Which line causes the error? – Code-Apprentice May 20 '18 at 18:55
  • java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at Schedule.fillArray(Schedule.java:45) at Schedule.(Schedule.java:19) at Test.main(Test.java:16) – Katrina Nygren May 20 '18 at 19:03
  • Please [edit] your question to include the error message. Also be sure to show which line of your code causes the error. – Code-Apprentice May 20 '18 at 19:13
  • Also, does your input file have a backslash and a letter en? Or do you have actual newline characters? – Code-Apprentice May 20 '18 at 19:13
  • The file had new line characters so it is structured one line at a time. I added a screenshot of the exact error message – Katrina Nygren May 20 '18 at 19:19
  • I have edited your question to format the input a little better. Check out the linked question and all of its answers. If you still need help after reading that, you should edit your question by copy-pasting the error message directly instead of posting a screenshot. Also indicate what line causes the error. – Code-Apprentice May 20 '18 at 19:41

0 Answers0