Drew Cheerio
Drew Cheerio

Reputation: 13

While-loop in Java not working

Alright, so the problem is this:

I want to enter a bunch of numbers, a random set, of say 5 numbers. At the end of that number, I'll add a sixth number, 0, which will be a signal for my loop to end.

To achieve this, this is what I'll do.

I run my program and this is my input:

1 3 4 2 5 0

So I want the program to print

1 3 4 2 5 "End of sequence"

Here's my code that will purportedly do such a thing (all of this is in main method):

Scanner input = new Scanner(System.in);
System.out.println("Alright, what?");
boolean notZero = true;
while (notZero)  
 {
   System.out.println(input.nextInt());
   if (input.nextInt() == 0)
       System.out.println("End of sequence");
       notZero = false;    
 }

These, however, are the results I get:

https://i.sstatic.net/DUFhs.jpg

Interestingly, if I don't have the while-loop stop, and I input the same number list again (1 3 4 2 5 0) it'll output 3 and 2, which are the even numbers in my number list - and all this vexes me further.

Any ideas?

Upvotes: 0

Views: 4141

Answers (7)

cHao
cHao

Reputation: 86505

Far as i see, notZero's only purpose is to terminate the loop. You don't need it if you use a loop that terminates on the loop condition. You also don't need to print the "End of sequence" inside the loop, since by definition, it prints once the sequence is ended.

If we test for the condition directly instead,

int n;
while ((n = input.nextInt()) != 0)
{
    System.out.println(n);
}
System.out.println("End of sequence");

You could also say for (int n = input.nextInt(); n != 0; n = input.nextInt()) instead of the while. It ensures that n is only visible within the loop, but it also means repeating yourself -- and leaving open the possibility that someone could make the two 'initialization' and 'increment' sections different later.

Upvotes: 0

Jason Dean
Jason Dean

Reputation: 9615

Your problem is here:

 if (input.nextInt() == 0)

When you call nextInt() twice it removes two numbers. You need to only call it once per iteration.

 while (notZero)  
 {
    int num = input.nextInt();
    System.out.println(num);

    if (num == 0) {
        System.out.println("End of sequence");
        notZero = false;    
    }

  }

Fix that along with the problem that Hovercraft Full of Eels pointed out and you should be good.

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532465

You are calling input.nextInt() twice in the loop, only printing one of them. In effect, that is like skipping every other number in the input. Change your code to store the next input into a variable, then check the variable. I suspect this isn't your actual code as without setting notZero to false as a result of checking the condition, it will only execute the loop once. FWIW, notZero is a lousy variable name. It should be semantic, not indicate its expected value. You'll also want to only print the input value if it isn't the end of input indicator.

Scanner input = new Scanner(System.in);

System.out.println("Alright, what?");

boolean endOfInput = false;

while (!endOfInput) {
   int next = input.nextInt();

   if (next == 0) {
      System.out.println("End of sequence");
      endOfInput = true;
   }  
   else {
      System.out.println(next);
   } 
}

Upvotes: 2

Dan
Dan

Reputation: 11069

This looks like a learning exercise, so I'll give you a few hints rather than tell you outright.

  1. In your IDE of choice, tell it to auto-indent your code. Look closely at where the if statement and its results fall.

  2. Think carefully about what nextInt() does and about where and how often you want to call it.

Upvotes: 2

Howard
Howard

Reputation: 39197

Besides the two reads inside your loop, I assume you want to have a block after your if-statement

if (input.nextInt() == 0) {
    System.out.println("End of sequence");
    notZero = false;    
}

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Wrap all your blocks including if blocks in curly braces!

This:

if (foo) 
  doSomething();
  doSomethingElse();

is not working as you think it is as it's behaving as

if (foo) {
  doSomething();
}

doSomethingElse();

Instead wrap all blocks in curly braces so that there is no doubt:

if (foo) {    
  doSomething();
  doSomethingElse();
}

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

You're calling nextInt() twice each time through the loop. That means that each time around, you'll consume two numbers, doing different things with each one -- i.e., the one you print and the one you compare to 0 are two different numbers. You need to call nextInt() just once and store the result in a variable, then operate on it.

Upvotes: 1

Related Questions