2

I'm trying to make an Insertion Sort algorithm in Java, and I want it to read user input, and he/she can put however many numbers they wish (We'll say they're all integers for now, but long run it would be nice to be able to do both integers and doubles/floats), and I want the algorithm to sort them all out. My issue is that when I run this code to see if the integers are adding correctly, my loop never stops.

public class InsertionSort {
public static void main(String[] args){
    System.out.println("Enter the numbers to be sorted now: ");
    ArrayList<Integer> unsortNums = new ArrayList<Integer>();
    Scanner usrIn = new Scanner(System.in);

    while(usrIn.hasNextInt()) {
        unsortNums.add(usrIn.nextInt());
        System.out.println(unsortNums);    //TODO: Doesn't stop here
    }
    sortNums(unsortNums);
  }
}

Now, I suspect it has something to do with how the scanner is doing the .hasNextInt(), but I cannot for the life of me figure out why it isn't stopping. Could this be an IDE specific thing? I'm using Intellij Idea.

Let me know if I left anything out that I need to include.

5
  • 1
    The question's got nothing to do with the IDE itself, so I've removed that tag. Commented Jan 28, 2015 at 7:15
  • 2
    What input are you providing that's problematic? Read the Scanner docs – nextInt() might be blocking waiting for input. How do you expect to break out of the loop? Commented Jan 28, 2015 at 7:16
  • What is the input you are testing this with? Commented Jan 28, 2015 at 7:18
  • Scanner.hasNextInt() waits until the user hits enter and checks than if the input is of type Integer. When the user enters something else, 'done' for example, Scanner.hasNextInt() returns false and the loop is exited. So give the user a hint, what to do after finishing his list of integers. Commented Jan 28, 2015 at 7:18
  • Makoto, that's fine, just added it to be safe. Barq, I just tried "1 2 3 4", without the quotes, obviously. Commented Jan 28, 2015 at 7:18

2 Answers 2

1

Your code will stop as long as you stop adding numbers to your input stream. nextInt() is looking for another integer value, and if it can't find one, it'll stop looping.

Give it a try - enter in any sequence of characters that can't be interpreted as an int, and your loop will stop.

As a for-instance, this sequence will cease iteration: 1 2 3 4 5 6 7 8 9 7/. The reason is that 7/ can't be read as an int, so the condition for hasNextInt fails.

Sign up to request clarification or add additional context in comments.

3 Comments

I wonder: Could I use a delimiter, say a comma, to separate the numbers and have the scanner check for all the commas, therefore, if the numbers entered were something like 1,2,3,4 it would add the numbers and stop when it stops seeing commas?
Why bother? Adding more friendly messages such as "enter a single period or the word stop when you're done entering numbers" is more straightforward and won't require any modifications to the actual reading logic.
Ok, I see what you mean. I guess I'll go about it like that then. Thanks!
0

When using a scanner on System.in, it just blocks and waits for the user's next input. A common way of handling this is to tell the user that some magic number, e.g., -999, will stop the input loop:

System.out.println("Enter the numbers to be sorted now (-999 to stop): ");
List<Integer> unsortNums = new ArrayList<Integer>();
Scanner usrIn = new Scanner(System.in);

int i = usrIn.nextInt();
while(i != -999) {
    unsortNums.add(i);
    i = usrIn.nextInt();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.