Question: Help Troubleshooting Java code, keep getting these errors Exception in thread main java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextLong(Scanner.java:2373) at java.base/java.util.Scanner.nextLong(Scanner.java:2328) at file.Project1.readFile(Project1.java:33) at file.Project1.main(Project1.java:200)

Help Troubleshooting Java code, keep getting these errors Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextLong(Scanner.java:2373) at java.base/java.util.Scanner.nextLong(Scanner.java:2328) at file.Project1.readFile(Project1.java:33) at file.Project1.main(Project1.java:200)

The chunk of code that i think is the issue is listed below

The code that keeps erroring out seems related to the Scanner that I am trying to use to read in a csv file. Where am I going wrong here?

// Driver class definition public class Project1 { // Scanner class object created static Scanner sc = new Scanner(System.in); // Creates an ArrayList to store country object static ArrayList Countries1 = new ArrayList(); // Method to red file contents static void readFile() { // try block begins try { // Opens the file for reading FileReader fr = new FileReader("Countries1.csv"); // Links the scanner class to file Scanner br = new Scanner(fr); // Loops till end of the file while (br.hasNext()) { // Extracts data from the file, using parameterized constructor // creates Country object and stores it in array list Countries1.add(new Country(br.next(), br.next(), br.nextLong(), br.nextDouble(), br.nextLong(), br.nextLong())); } // End of while loop // Close the file br.close(); }// End of try block // Catch block to handle FileNotFoundException catch (FileNotFoundException fe) { System.out.print("ERROR: Unable to open the file for reading."); }// End of catch block }// End of method

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!