Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

do/while loops not working correctly

Status
Not open for further replies.

Zigen

IS-IT--Management
Jan 20, 2003
6
0
0
US
While running this program I want to input numbers that have the following rules: (1) no negative numbers (2) smallValue < middleValue < largeValue. If either one of the rules are broken when I input a number into each one, then it will pop up a error message, and make them re-input a value.

The trouble that I am having with my program is that when I test the middle do/while loop. If I set my smallValue = 5, and I also set my middleValue = 5. It will pop the error message up to re-enter a value, but instead of letting me re-enter a new middleValue, it will go on to the next input message for largeValue. But when I test the middleValue against a negative number, then it will prompt the message, and make me reenter a valid number. The same things happen for my largeValue do/while loop.

I figured that I could go ' while (!(middleValue < 0 && middleValue <= smallValue)); ' it will switch my current situation around.

How can I fix my problem? Thanks in advance for peoples input!

public static void main(String args[])
{
String inputSmall, inputMiddle, inputLarge, inputDistance;
int smallValue = 0, middleValue = 0, largeValue = 0, distanceValue = 0;

// Test valid numbers inputed
// No negative numbers
// small < middle < large

// small
do {
inputSmall = JOptionPane.showInputDialog(&quot;Small step value?&quot;);
smallValue = Integer.parseInt(inputSmall);
if (smallValue >= 0) continue;
JOptionPane.showMessageDialog(null, &quot;Re-enter small step value&quot;, &quot;Error&quot;, JOptionPane.ERROR_MESSAGE);
}
while (smallValue < 0);

// middle
do {
inputMiddle = JOptionPane.showInputDialog(&quot;Middle step value?&quot;);
middleValue = Integer.parseInt(inputMiddle);
if (middleValue >= 0 && middleValue > smallValue) continue;
JOptionPane.showMessageDialog(null, &quot;Re-enter middle step value&quot;, &quot;Error&quot;, JOptionPane.ERROR_MESSAGE);
}
while (middleValue < 0 && middleValue <= smallValue);

// large
do {
inputLarge = JOptionPane.showInputDialog(&quot;Large step value?&quot;);
largeValue = Integer.parseInt(inputLarge);
if (largeValue >= 0 && largeValue > middleValue && middleValue > smallValue && largeValue > smallValue) continue;
JOptionPane.showMessageDialog(null, &quot;Re-enter large step value&quot;, &quot;Error&quot;, JOptionPane.ERROR_MESSAGE);
}
while (largeValue < 0 && largeValue <= middleValue && middleValue <= smallValue && largeValue <= smallValue);

// distance
do {
inputDistance = JOptionPane.showInputDialog(&quot;Distance value?&quot;);
distanceValue = Integer.parseInt(inputDistance);
if (distanceValue >= 0 ) continue;
JOptionPane.showMessageDialog(null, &quot;Re-enter distance value&quot;, &quot;Error&quot;, JOptionPane.ERROR_MESSAGE);
}
while (distanceValue < 0);



// Creating new Robot and printing it on a JOptionPane
JOptionPane.showMessageDialog(null, &quot;Number of ways the robot can move a distance of &quot; + distanceValue +
&quot; taking steps of length &quot; + smallValue + &quot;, &quot; + middleValue + &quot;, &quot; + largeValue + &quot; is: &quot; + now(smallValue, middleValue, largeValue, distanceValue),
&quot;Number of ways&quot;, JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
 
Erm, this are rather strange loops you did there.
Try this (as a modified example of the first check):

do {
inputSmall = JOptionPane.showInputDialog(&quot;Small step value?&quot;);
smallValue = Integer.parseInt(inputSmall);
if (smallValue<0)
JOptionPane.showMessageDialog(null, &quot;Re-enter small step value&quot;, &quot;Error&quot;, JOptionPane.ERROR_MESSAGE);
} while (smallValue < 0);
 
On thing which isnt related to your question, but is a comment on your code is the way you are using testing for the middle value and the large value.
In both cases you are testing to see if the value is greater then 0 and greater then the last number. But since the small number test you did has already checked for 0 why do it again?

first test -
Code:
(smallValue > 0)
second test -
Code:
(middleValue < 0 && middleValue <= smallValue)
But to get to that second test smallValue MUST be greater then 0, so if
Code:
middleValue <= smallValue
is true then
Code:
middleValue < 0
MUST be true by default and if
Code:
middleValue <= smallValue
is false it doesnt matter what the result of
Code:
middleValue <= smallValue
is, so don't bother to check for it.

Its just an efficency thing.
Makes for neater code too. -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
Instead of the line &quot;while (middleValue < 0 && middleValue <= smallValue);&quot; I'd use &quot;while (middleValue <= smallValue)&quot; If you really want to check middleValue against 0, you need ||, not && to keep the loop going.

Rose/Miros
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top