2

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)
Abra
  • 11,631
  • 5
  • 25
  • 33

3 Answers3

4

Setting the delimiter for class java.util.Scanner to comma (,) means that each call to method next() will read all the data up to the next comma, including newlines. Hence the call to nextInt reads the score plus the name on the next line and that isn't an int. Hence the InputMismatchException.

Just read the entire line and split it on the comma (,).
(Note: Below code uses try-with-resources)

public class ReadData {
    public static void main(String[] args) throws Exception {
        java.io.File file = new java.io.File("scores.txt");
        try (Scanner input = new Scanner(file)) {
//            input.useDelimiter(","); <- not required
            while (input.hasNextLine()) {
                String line = input.nextLine();
                String[] parts = line.split(",");
                String name1 = parts[0];
                int score1 = Integer.parseInt(parts[1].trim());
                System.out.println(name1+" "+score1);
            }
        }
    }
}
Abra
  • 11,631
  • 5
  • 25
  • 33
0

Use ",|\\n" RegExp delimiter:

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(",|\\n");
    while (input.hasNext()) {
        String name1 = input.next();
        int score1   = Integer.parseInt(input.next().trim());
        System.out.println(name1+" "+score1);
    }
    input.close();
  }
}
Sergey Afinogenov
  • 1,444
  • 2
  • 7
  • I was very close to having the same code, only issue was I didn't invoke the trim method so I got a numberformatexception but thanks anyway. – Anesu Jambwa Apr 22 '21 at 11:13
0

Try this.

String text = "John T Smith, 90\r\n"
            + "Eric K Jones, 85";
Scanner input = new Scanner(text);
input.useDelimiter(",\\s*|\\R");
while (input.hasNext()) {
    String name1 = input.next();
    int score1 = input.nextInt();
    System.out.println(name1+" "+score1);
}
input.close();

output:

John T Smith 90
Eric K Jones 85
saka1029
  • 13,523
  • 2
  • 13
  • 37