0

My input is in this format:

1 2 3 4 5 6
Alice

The array length is not known. I coded it this way:

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int i=0;
        while(sc.hasNext()){
            arr.add(sc.nextInt());
        }
        String player = sc.nextLine();
    }
}

But I am getting this error.

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.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at Main.main(Main.java:17)

Thanks in advance.

Topaco
  • 40,594
  • 4
  • 35
  • 62
lsr
  • 264
  • 1
  • 10
  • 3
    Hint: have you debugged through the code? How far does it get? When do you expect `hasNextLine()` to return false? – Jon Skeet Jul 05 '22 at 18:24
  • It's an infinte loop even after reading 6. How to come out of the `while` loop? – lsr Jul 05 '22 at 18:35
  • I would suggest just reading the first line as a whole, then splitting it on spaces and parsing each part separately. `Scanner` is remarkably hard to use "properly". – Jon Skeet Jul 05 '22 at 18:48
  • Thanks for the reply Jon Skeet. What's the most efficient way to take such kind of input? – lsr Jul 05 '22 at 18:53

2 Answers2

2

The reason you are seeing java.util.InputMismatchException is because you provided input "Alice" to the statement sc.nextInt(), and the scanner is telling you that it doesn't know how to convert a java.lang.String input to an int (which is the return type of nextInt()).

Here's a very simple example that reproduces the same behavior:

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

If you run those two lines and enter a non-integer, like "d", it will throw an exception:

d
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.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at a91.main(a91.java:6)

To fix it, you need to replace nextInt() with something that is tolerant of non-numeric input, possibly nextLine(). Here is a (very) simple example showing how that could work. Note this is just highlighting the behavior you're asking about, namely: how to address InputMismatchException. As with your original program, there is no loop termination – it will run forever (until you quit the program).

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();

while (sc.hasNext()) {
    String s = sc.nextLine();
    System.out.println("got this: " + s);
}

1 2 3 4 5 6
got this:  2 3 4 5 6
Alice
got this: Alice
Kaan
  • 5,434
  • 3
  • 19
  • 41
  • Thanks for the reply. I wanted to know is there a way to terminate the loop. I tried using `sc.close()` but it didn't help either. And what's the best way to take these kind of inputs? – lsr Jul 05 '22 at 18:47
  • re: _"what's the best way to take these kind of inputs?"_ – Did you read my answer? re: _"I wanted to know is there a way to terminate the loop"_ – Where is that mentioned in your posted question? If your question is "how do I exit the loop", the entire thing should be rewritten to make the question clear, and remove anything about data types, input mismatch, etc – Kaan Jul 05 '22 at 18:58
  • Thanks for the detailed answer @Kaan Your approach has solved my issue. I just wanted to know further if there was a way to stop the infinite loop? Otherwise my doubt is already solved. – lsr Jul 05 '22 at 19:06
  • _"Your approach has solved my issue"_ – Ok great. When someone posts an answer that addresses your question, you should vote up and accept the answer _ this lets everyone know the question has been resolved. – Kaan Jul 05 '22 at 19:09
  • To exit the loop, you can add code to check for a specific value – maybe the string "exit" – and then break out of the loop (using `break`) if that value is seen. – Kaan Jul 05 '22 at 19:12
1

You should use hasNextInt to check for integer input. Once no more integers, then just use next() to read the player.

List<Integer> arr = new ArrayList<>();
Scanner sc = new Scanner(System.in);

while(sc.hasNextInt()){
    arr.add(sc.nextInt());
}
String player = sc.next();

arr.forEach(System.out::println);
System.out.println(player);

Example input's supported

10 20 30 40 50 60 70
Alice

10 20 30 40
50 60 70 Alice

10 20 30
40
50
60 70 Alice

10 20 30
40 50
60 70
Alice

output

10
20
30
40
50
60
70
Alice
WJS
  • 36,363
  • 4
  • 24
  • 39