I have a text file where I am trying to read string and integer input using a Scanner
. I need to separate the data using a comma and there is also the issue of newline character. Here is the text file contents:
John T Smith, 90
Eric K Jones, 85
My code:
public class ReadData {
public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File("scores.txt");
Scanner input = new Scanner(file);
input.useDelimiter(",");
while (input.hasNext()) {
String name1 = input.next();
int score1 = input.nextInt();
System.out.println(name1+" "+score1);
}
input.close();
}
}
Exception:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at ReadData.main(ReadData.java:10)