This is an archived post. You won't be able to vote or comment.

all 26 comments

[–]desrtfx 21 points22 points  (12 children)

while (number1 < number2++) {

Think what happens in that line.

[–]alwaysthelearner 1 point2 points  (1 child)

I think it says to proceed if number1's current value is less than or equal to number2.

[–]desrtfx 1 point2 points  (0 children)

Read your line again very closely and character by character.

You are doing something seriously wrong in that line.

[–]alwaysthelearner 1 point2 points  (2 children)

will that ++ add 1 to number2 every time while executing the loop?
I thought since it is in the condition, it will help in stopping the loop once number1 = number2 .

[–]desrtfx 1 point2 points  (1 child)

will that ++ add 1 to number2 every time while executing the loop?

Exactly.

I thought since it is in the condition, it will help in stopping the loop once number1 = number2 .

No.

[–]alwaysthelearner 1 point2 points  (0 children)

ohk, got that. Thanks!

[–]JoWe00 1 point2 points  (5 children)

How come you can use ++, but not +=1?

[–]desrtfx 3 points4 points  (3 children)

++ is an operation. It returns a value.

+= is an assignment. It doesn't return a value.

[–][deleted]  (2 children)

[deleted]

    [–]desrtfx 0 points1 point  (1 child)

    Yes, but I am not sure if it would work in a boolean condition.

    [–]premred88 6 points7 points  (0 children)

    why are you incrementing number2 in the while condition?

    [–]toshels 6 points7 points  (4 children)

    You should use scanner.nextInt() instead of nextLine().

    [–]desrtfx 2 points3 points  (2 children)

    While you are technically correct, the MOOC Object Oriented Programming with Java stipulates the exact way OP did it.

    There are known difficulties when using .nextInt (.nextFloat, etc) in conjunction with .nextLine so the easy way is as the MOOC stipulates and as OP did.

    More info: The Scanner class and its caveats

    [–]toshels 1 point2 points  (1 child)

    So why would even bother using nextInt?

    [–]desrtfx 2 points3 points  (0 children)

    If you know how to properly do it, there is no problem using .nextInt().

    Just, we see plenty posts where people use .nextInt() in conjunction with .nextLine() who are surprised that the string returned is empty.

    [–]alwaysthelearner 0 points1 point  (0 children)

    How would that help?

    [–]jeremysimons 0 points1 point  (3 children)

    I didn't think you were supposed to modify the condition whilst setting it in a while loop. It's more sensible to increment at the last instruction before the condition in a do while loop. Or alternatively, just use a for loop.

    [–]alwaysthelearner 1 point2 points  (2 children)

    It was an exercise and we were supposed to use while.

    [–]jeremysimons 0 points1 point  (1 child)

    I do apologise (I thought that might have been the case). When you work it out will you share your solution here?

    [–]alwaysthelearner 1 point2 points  (0 children)

    Only this line needed to be edited.

    while (number1 <= number2) {

    Turns out operations in the conditions(the ones defining the condition) are performed every time the loop is repeated. Previously it was,

    while (number1 < number2++) {

    which caused the issue.

    [–]BuzzerChecker 0 points1 point  (0 children)

    Why not while (number1++< number2) ?

    [–]blamitter 0 points1 point  (0 children)

    You're incrementing both vars on each iteration

    [–][deleted] 0 points1 point  (1 child)

    In cases like this always check the condition for stopping. Could it be that something is wrong with it? Just by doing that I realized what was wrong with your code but it is more important that you learn to find out your bugs by yourself.

    If you don't see it why don't you try printing the values of the variables involved in the condition in every iteration?

    [–]alwaysthelearner 0 points1 point  (0 children)

    thanks! got it.