File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes program assignment help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "program assignment help " Watch "program assignment help " New topic

program assignment help

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
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 ]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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

Joined: Jul 08, 2003
Posts: 24199

"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 "=".

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: program assignment help
jQuery in Action, 3rd edition