This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
In this assignment I have to write a program which accepts an integer between 10,000 and 99,999 then strips off the individual numbers and displays them along with their sum. I used a while loop and created the value InOut, so that the user can opt to press 1 to continue and be able to input another integern, or press 2 (technically any other number would work as well) to quit the loop. Within the loop theres an if else statement which will display an error message if a value that is not between 10,000 and 99,999 is entered. I wrote the program, and I don't see anything wrong with it but I keep getting an error. Here's my code, along with the error message.
I haven't the faintest clue why its telling me a boolean is required. Any assistance in getting this to work would be greatly appreciated. [ edited to format code to not have a big long unbroken line -ds ] [ October 31, 2003: Message edited by: Dirk Schreckmann ]
The "=" operator is for assignment. The "==" operator is for doing comparisons. In Java the assignment operator returns a value, namely the value being assigned. Thus you can do: int one = two = three = 0; Which will set one, two and three to zero (0). First "three" is set to zero, and the result of that assignment (namely, zero) is assigned to two, etc. So when you do "while (InOut = 1)" what happens is that the value one (1) is assigned to "InOut" and the result--one--is being used in place of a boolean. Since this is illegal you get the compile error. Congratulations, you stumbled across one of the most common errors in Java, one that has tripped up probably every Java programmer at one time or another ;-) [ October 31, 2003: Message edited by: Wayne L Johnson ]