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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started