aspose file tools*
The moose likes Beginning Java and the fly likes Ending  a do while statement in this situation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ending  a do while statement in this situation?" Watch "Ending  a do while statement in this situation?" New topic
Author

Ending a do while statement in this situation?

jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
How do I end do while statement in this situation: I have a switch statement and in one of my cases I do a calculation. The condition t says enter a number, if it is positive you go on. If it is negative it continues back to the top to enter a positive variable. Then I have my while statement says that condition <= 0 then it go on and to the next do while statement. So then I enter the second number for the calculation it suppose to do the same thing if it is negative tells me to enter positive variable it does not do that. Then on the last while I have the same condition <= 0. Then right under the while statement I have formula.
I have tired all kinds of things to stop the last while statement to make sure the number is positive before it reads the formula. I entered a break then is says statement not reachable. Or is there some way to make sure it looks at the last while before moving on to the formula.
Sorry this is so long but if any one can help a little that would be greatly appreciated also if any go web sites to look java help on.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
Would it help if I put the program in here?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yes, but keep it to the essentials.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sangeeta Murthy
Greenhorn

Joined: Feb 19, 2003
Posts: 3
Is it possible that you are using the wrong variable in the last while loop to check the terminating condition? It could be possible because you are using many while loops.
jon ladd
Ranch Hand

Joined: Feb 11, 2003
Posts: 53
Here is a piece of the code, the last do while statement should also check to see if number is negtive. Instead it takes whatever number postive or negtive for width and just takes the number and does equation.
case RECTANGLE: {
double area;
double length;
double width;
Utility.separator(80, '*');
do {
System.out.print(" Enter length: ");
length = Keyboard.readDouble();
Utility.skip();
if (length <= 0 ) {
System.out.println("Invalid
length");

continue;
}
}
while (length <= 0 );
do {

System.out.print(" Enter width: ");
width = Keyboard.readDouble();
Utility.skip();
if (width <= 0) {
System.out.println("Invalid
width");
continue;
}
}
while ( width <= 0);

area = length * width;
System.out.println(" Rectanlge area
= " + area);
Utility.skip();
Utility.separator(80, '*');
break;
}
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I modified your code so that I could actually run it and took away the redundant continue statements:

The output is what I expected:

Invalidlength -4.0
Invalidlength -3.0
Invalidlength -2.0
Invalidlength -1.0
Invalidlength 0.0
Invalidwidth -4.0
Invalidwidth -3.0
Invalidwidth -2.0
Invalidwidth -1.0
Invalidwidth 0.0
length = 1.0 width = 1.0
Rectangle area = 1.0

No problem
[ February 21, 2003: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ending a do while statement in this situation?