-4
//input: multiple integers with spaces inbetween
    Scanner sc = new Scanner(System.in);
    while(sc.hasNextInt())
    {
      //add number to list
    }

sc.hasNextInt() is waiting for an integer. It only breaks out if you input a non-integer character.

I saw a solution here before not too long ago but i cant find it anymore.

The solution (was the best if you ask me) was using two scanners. I cant seem to figure out how it used two scanners to go around this problem.

sc.NextLine() maybe?


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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user3603865
  • 57
  • 1
  • 8
  • You cannot use this if you want it to exit at some point - this code will block _forever_ on the input. How do you want you program to know when input is done? – Boris the Spider Oct 12 '15 at 14:58
  • What is `sc` based on? Console input? – PM 77-1 Oct 12 '15 at 14:58
  • 3
    It looks like you accidentally deleted the first half of your question before posting. Can you please add it back? – djechlin Oct 12 '15 at 14:58
  • Using 2 scanners seems unnecessarily complicated. Did you consider changing the delimiter [Example](http://stackoverflow.com/a/26567018/1413133) – Manos Nikolaidis Oct 12 '15 at 15:01
  • 3
    I still don't know what you want to do. What is the question? – TheLostMind Oct 12 '15 at 15:02
  • The thing is, i just want to know how the two scanners was used. I saw it before and i just want to feed my curiosity. So my question again is: how can i use hasNextInt() with two scanners. Without changing the delimiter or reading the whole line and splitting on spaces and such. Just two scanner. – user3603865 Oct 12 '15 at 15:06
  • Using two scanners doesn't have any sense if you are reading from `System.in`. Also `hasNextint` has to wait for user input since `System.in` is opened stream, which means that next value is currently being created (like it client may be in the middle of writing data which we want to read) so Scanner needs to wait for it. – Pshemo Oct 12 '15 at 15:10
  • You need to be more precise about what problem you are trying to solve. What you want to achieve exactly? – Pshemo Oct 12 '15 at 15:11
  • 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. – user3603865 Oct 12 '15 at 15:18

3 Answers3

0

Try this:

while (sc.hasNext()) {
    if (sc.hasNextInt()) {
       // System.out.println("(int) " + sc.nextInt());
       // or add it to a list
    }
     else {
       // System.out.println(sc.next());
       // or do something
    }
}
Scitech
  • 513
  • 1
  • 5
  • 16
0
    Scanner sc = new Scanner(System.in);
    while(sc.hasNextInt())
    {

        System.out.println("Hi");
        System.out.println("Do you want to exit ?");
        Scanner sc2 = new Scanner(System.in);
        if(sc2.next().equalsIgnoreCase("Yes")){
            break;
        }
     }
Rahman
  • 3,755
  • 3
  • 26
  • 43
0

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);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • This is exactly what i ment. This is what i wanted to know how a second scanner was used to feed my curiosity. Thank you. Another quick question, what is the best practice to handle this? – user3603865 Oct 12 '15 at 15:28
  • There is no best solution, because solutions depends on how you want your code to work. Best practices suggest to write code which is easy to work with first, and only change it when it causes system to work slowly so I think that this solution should work fine for you. – Pshemo Oct 12 '15 at 15:34