:
Beginning Java
program assignment help
Brandi Love
Ranch Hand
Posts: 133
posted 15 years ago
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.
import java.io.*; import java.text.*; public class IntegerSum { public static void main(String[] args) throws java.io.IOException { String s1, s2; int InputNum, num5, num4, num3, num2, num1, sum, InOut; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); DecimalFormat num = new DecimalFormat(",000"); InputNum = 0; num5 = InputNum % 10; num4 = (InputNum / 10) % 10; num3 = (InputNum / 100) % 10; num2 = (InputNum / 1000) % 10; num1 = (InputNum / 10000); sum = num1 + num2 + num3 + num4 + num5; InOut = 1; while (InOut = 1) { System.out.println("Please enter a number between 10000 and 99999:"); s1 = br.readLine(); InputNum = Integer.parseInt(s1); if (InputNum >= 10000 && InputNum <= 99999) { System.out.print("You entered " + num.format(InputNum) + ". \nThe sum of " + num1 + ", " + num2 + ", " + num3 + ", " + num4 + ", and " + num5 + " is " + sum + ".\n"); } else { System.out.println("Invalid entry"); } System.out.println("Enter 1 to continue or 2 to exit"); s2 = br.readLine(); InOut = Integer.parseInt(s2); } } }
Error:
C:\Documents and Settings\Administrator.HD5\Desktop\IntegerSum.java:30: incompatible types found : int required: boolean while (InOut = 1) ^
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
posted 15 years ago
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
Posts: 24203
43
I like...
posted 15 years ago
"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]
It is sorta covered in the
JavaRanch Style Guide
.
Post Reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
display number xxx as "x x x " method
One more question, then thats it I promise :) ...
random floating point and truncate rrors in java
Program Error,Thanks
recursive method
More...