Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

program assignment help

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Error:

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 ]
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"InOut = 1" is a statement, assigning a value to the variable. You want to compare the value and the variable for equality using "==", not the assignment operator "=".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic