Anesu Mazvimavi
Anesu Mazvimavi

Reputation: 139

Use delimiter to separate a pattern

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)

Upvotes: 2

Views: 564

Answers (3)

Abra
Abra

Reputation: 20914

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);
            }
        }
    }
}

Upvotes: 4

user4910279
user4910279

Reputation:

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

Upvotes: 0

Sergey Afinogenov
Sergey Afinogenov

Reputation: 2202

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();
  }
}

Upvotes: 0

Related Questions