This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

program assignment help

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.


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 ]
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 ]
author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • 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 "=".
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!