[Solved] HasNextInt() Infinite loop [closed]


Based on your comment

A user can input multiple integers the amount is unknown. Ex: 3 4 5 1. There is a space inbetween them. All i want to do is read the integers and put it in a list while using two scanners.

You are probably looking for:

  • scanner which will read line from user (and can wait for next line if needed)
  • another scanner which will handle splitting each number from line.

So your code can look something like:

List<Integer> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.print("give me some numbers: ");
String numbersInLine = sc.nextLine();//I assume that line is in form: 1 23 45 

Scanner scLine = new Scanner(numbersInLine);//separate scanner for handling line
while(scLine.hasNextInt()){
    list.add(scLine.nextInt());
}
System.out.println(list);

2

solved HasNextInt() Infinite loop [closed]