 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
da Vinci Guest
|
Posted: Sun Aug 22, 2004 6:39 pm Post subject: LOOP Statements Help: Not working right..... |
|
|
I completed an assignment for my Intro to Java Programming class and
then continually updated the program to make it better. This is the
third or fourth incarnation of it and I have run into a problem.
Inside of a while loop, which is controlled by a boolean (repeat), I
have an if statement. Basically, if the user enters any positive
integer, it will output the area and volume of a cube with the side
length of the entered integer. It continually loops until the user
enters 0 for the side length, which will then set repeat to false and
end the loop statement.
However, it is not ending the loop. I output the integer value to
ensure it is setting to 0 correctly and it is. However, it still wont
end the while loop. The only way I am able to end the while loop is by
adding return; into the if statements. Then it will end. But nothing
in the book shows this as being the 'correct' way to do this.
I left the return; statement in the code for anyone to peek at to see
what is going on. Any help is appreciated (Thanks in advance!).
Code follows:
import java.io.*;
public class CubeUserAdv
{
public static void main(String argv[]) throws IOException
{
int cubevalue=1;
boolean repeat = true;
// Code to get interger inputs from a string input.
BufferedReader input = new BufferedReader (new
InputStreamReader(System.in));
String inputString;
System.out.println("nnThis program will display the surface area
and volume for three cubes.");
System.out.println("Ensure that the numbers you enter for cubes are
positive integers.nn");
// While loop to get values for the cubes & display output.
while (repeat = true)
{
System.out.println("nnEnter a value for cube (0 to end): ");
inputString = input.readLine();
cubevalue = Integer.parseInt(inputString);
if (cubevalue == 0)
{
repeat = false;
return;
}
else
{
repeat = true;
}
Cube cuber = new Cube(cubevalue);
// Data Output to Console.
System.out.println("n******** Cube ********");
System.out.println("Surface Area = " + cuber.Calc_SA());
System.out.println("Volume = " + cuber.Calc_Volume());
System.out.println(cubevalue);
}
System.out.println("nn");
}
}
******* NEW FILE *********
public class Cube
{
private double Side_Length;
public Cube(double SL_Input)
{
Side_Length = SL_Input;
}
public double Calc_SA()
{
return (6 * (Side_Length * Side_Length));
}
public double Calc_Volume()
{
return ((Side_Length * Side_Length) * Side_Length);
}
}
|
|
| Back to top |
|
 |
Paul Lutus Guest
|
Posted: Sun Aug 22, 2004 7:13 pm Post subject: Re: LOOP Statements Help: Not working right..... |
|
|
da Vinci wrote:
| Quote: | I completed an assignment for my Intro to Java Programming class and
then continually updated the program to make it better. This is the
third or fourth incarnation of it and I have run into a problem.
Inside of a while loop, which is controlled by a boolean (repeat), I
have an if statement. Basically, if the user enters any positive
integer, it will output the area and volume of a cube with the side
length of the entered integer. It continually loops until the user
enters 0 for the side length, which will then set repeat to false and
end the loop statement.
However, it is not ending the loop. I output the integer value to
ensure it is setting to 0 correctly and it is. However, it still wont
end the while loop. The only way I am able to end the while loop is by
adding return; into the if statements. Then it will end. But nothing
in the book shows this as being the 'correct' way to do this.
|
I suggest that you think about what is happening in your code. Go through
it, line by line, and ask yourself the circumstances under which that line
will or will not be executed.
Notice about your code that, until you applied the "return" patch, the block
that displays the new cube values was executed whether or not the new
cubevalue is equal to zero. It should not be allowed to execute if the user
enters a zero. This is why you had to put in a return to stop the program.
Instead, place the display code inside your control structure.
Also, in this case, you want to use "do ... while", not "while" (sample code
below). You want to test the cubeValue variable at the *end* of the control
section, not the *beginning*.
Finally, when a program does something you do not expect, do not immediately
try to apply a patch to fix it. Instead, figure out why it did what it did.
This is what separates programmers from typists, and I know you want to be
a programmer.
Sample code (Test.java):
**************************************************
import java.io.*;
public class Test {
static public void main(String[] args) throws IOException
{
BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
int cubeValue;
do {
System.out.print("Enter a number (0 = quit):");
String inputString = input.readLine();
cubeValue = Integer.parseInt(inputString);
if(cubeValue != 0) {
System.out.println("A valid number was entered: " + cubeValue);
// your code here
}
else {
System.out.println("Quitting now.");
}
}
while(cubeValue != 0);
}
}
--
Paul Lutus
http://www.arachnoid.com
|
|
| Back to top |
|
 |
Tor Iver Wilhelmsen Guest
|
Posted: Sun Aug 22, 2004 7:39 pm Post subject: Re: LOOP Statements Help: Not working right..... |
|
|
da Vinci <blank (AT) blank (DOT) com> writes:
| Quote: | while (repeat = true)
|
Count the number of '=' characters in that while...
| Quote: | if (cubevalue == 0)
|
.... compared to that if.
I believe if you use 1.5's built-in "linter", it should warn you
sternly against the expression in the while condition.
|
|
| Back to top |
|
 |
da Vinci Guest
|
Posted: Sun Aug 22, 2004 8:39 pm Post subject: Re: LOOP Statements Help: Not working right..... |
|
|
On Sun, 22 Aug 2004 12:13:03 -0700, Paul Lutus <nospam (AT) nosite (DOT) zzz>
wrote:
| Quote: | I suggest that you think about what is happening in your code. Go through
it, line by line, and ask yourself the circumstances under which that line
will or will not be executed.
Notice about your code that, until you applied the "return" patch, the block
that displays the new cube values was executed whether or not the new
cubevalue is equal to zero. It should not be allowed to execute if the user
enters a zero. This is why you had to put in a return to stop the program.
Instead, place the display code inside your control structure.
Also, in this case, you want to use "do ... while", not "while" (sample code
below). You want to test the cubeValue variable at the *end* of the control
section, not the *beginning*.
|
I was using a do-while at first but, when it wasnt working, I went to
a while loop to see if there was a problem in the way I was coding the
do-while. I remember that stuff from my C++ class but was afraid Java
did it differently (it doesnt).
| Quote: | Finally, when a program does something you do not expect, do not immediately
try to apply a patch to fix it. Instead, figure out why it did what it did.
This is what separates programmers from typists, and I know you want to be
a programmer.
|
That 'return' was a last ditch effort before coming onto the usenet
for help. I sat here for an hour trying different things trying to get
it figured out. I agree: patches are not a solution to problems.
Properly coded statements are. Fully agree there... but this was a
last ditch effort and I knew it was incorrect.
Not only that, but I noticed that the return ended the program
immidiatly and did not continue with the stuff outside of the while
loop at the end. That is good to know that it will do that when used
in the main() class.
The other reply was right on with the problem... I had it as while
(repeat = true) instead of using ==. Something I overlooked the
hundred times I was doing the step-wise look over to see what was
going on.
Now time to change it back over to the do-while as you suggest and
then see what more I could do to advance the code.
Thanks!
|
|
| Back to top |
|
 |
da Vinci Guest
|
Posted: Sun Aug 22, 2004 8:40 pm Post subject: Re: LOOP Statements Help: Not working right..... |
|
|
On 22 Aug 2004 21:39:19 +0200, Tor Iver Wilhelmsen
<tor.iver.wilhelmsen (AT) broadpark (DOT) no> wrote:
| Quote: | da Vinci <blank (AT) blank (DOT) com> writes:
while (repeat = true)
Count the number of '=' characters in that while...
if (cubevalue == 0)
... compared to that if.
I believe if you use 1.5's built-in "linter", it should warn you
sternly against the expression in the while condition.
|
Jeez... I looked over that about a hundred times over the span of an
hour trying to figure this out. That did the trick.
Thanks for the help.
Chalk another one up for semantic errors!!!
|
|
| Back to top |
|
 |
Dhananjay Singh Guest
|
Posted: Mon Aug 23, 2004 11:59 am Post subject: Re: LOOP Statements Help: Not working right..... |
|
|
The problem is with statement ........
while (repeat = true) { ...
this is creating the problem. change it to "equals to" operator.
while(repeat == true) [....
It will work.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|