Need help with my while loops please do not know what I'm doing wrong
I am having a couple issue with my while loops...I cannot exit my first while loop even when I enter the correct information and if i cancel out the entire first while loop I escape the second loop no matter what after my second entry...here is just the test portion of my code:
Code:
import java.util.*;
public class TestSailBoat
{
public static void main(String[] args)
{
//Attributs
int _numSails;
int _lengthBoat;
String _colour;
//allows user to enter information about the boat
Scanner scanner = new Scanner(System.in);
System.out.print("Colour of boat: ");
_colour = scanner.nextLine();
while((!_colour.equals("red"))||(!_colour.equals("blue"))||(!_colour.equals("white"))||(!_colour.equals("yellow")))
{
System.out.println("ERROR: color must be red, white, blue or yellow");
System.out.print("Colour of boat: ");
_colour = scanner.nextLine();
}
System.out.print("Lenght of boat: ");
_lengthBoat = Integer.parseInt(scanner.nextLine());
while ((_lengthBoat < 0) && (_lengthBoat > 50));
{
System.out.println("ERROR: length of boat must be between 0 and 50 inclusively");
System.out.print("Lenght of boat: ");
_lengthBoat = Integer.parseInt(scanner.nextLine());
}
System.out.print("Number of sails: ");
_numSails = Integer.parseInt(scanner.nextLine());
while (_numSails < 1 && _numSails > 4)
{
System.out.println("ERROR: number of sails must be between 1 and 4 inclusively");
System.out.print("Number of sails: ");
_numSails = Integer.parseInt(scanner.nextLine());
}
SailBoat sails = new SailBoat(_colour, _lengthBoat, _numSails);
sails.calcPrice();
System.out.println(sails.toString());
}
}
Results:
First Loop:
Colour of boat: red
ERROR: color must be red, white, blue or yellow
Colour of boat: white
ERROR: color must be red, white, blue or yellow
Colour of boat: blue
ERROR: color must be red, white, blue or yellow
Colour of boat:
Second Loop:
Colour of boat: white
Lenght of boat: 98
ERROR: length of boat must be between 0 and 50 inclusively
Lenght of boat: 98
Number of sails:
I have not even tried testing the third loop yet
Re: Need help with my while loops please do not know what I'm doing wrong
You are using the wrong operator there. When you are using the || operator when one expression is true then the result will be true.
Code:
colors
-------------------------
!r !w !b !y
inputs
red F T T T = T
white T F F F = T
blue T T F T = T
yellow T T T F = T
Your other while loop also have a wrong expression, where the condition will never be true. No number is less and greater than something at the same time.
Re: Need help with my while loops please do not know what I'm doing wrong
Thank you wsaryada. I updated the second and third while loops in my code to be || instead of &&. That issue seems to be resolved now. I do not understand what type of operator I should use for the colors. I though it would be or (||) but your reasoning definitely makes sense (taking discrete math currently). What would you recommend?
Re: Need help with my while loops please do not know what I'm doing wrong
De Morgan already solved this small problem in the 19th century: if you want the colour to be one of red, white, blue or yellow, we can write it down an red || white || blue || yellow. If the colour isn't one of those colours, the expression !(red || white || blue || yellow) is true; De Morgan figured out that the last expression is equivalent to !red && !white && !blue && !yellow.
kind regards,
Jos
Re: Need help with my while loops please do not know what I'm doing wrong
thank you so much JosAH that worked for the colors and now for some reason the lengthBoat loop is not working:
Code:
System.out.print("Lenght of boat: ");
_lengthBoat = Integer.parseInt(scanner.nextLine());
while (_lengthBoat < 0 || _lengthBoat > 50);
{
System.out.println("ERROR: length of boat must be between 0 and 50 inclusively");
System.out.print("Lenght of boat: ");
_lengthBoat = Integer.parseInt(scanner.nextLine());
}
System.out.print("Size of engine: ");
_sizeOfEngine = Integer.parseInt(scanner.nextLine());
while (_sizeOfEngine < 1 || _sizeOfEngine > 350)
{
System.out.println("ERROR: size of engine must be between 1 and 350 inclusively");
System.out.print("Size of engine: ");
_sizeOfEngine = Integer.parseInt(scanner.nextLine());
}
my input:
Colour of boat: red
Lenght of boat: 34
ERROR: length of boat must be between 0 and 50 inclusively
Lenght of boat: 65
Number of sails:
Re: Need help with my while loops please do not know what I'm doing wrong
Quote:
Originally Posted by
ndsmith20
Code:
while (_lengthBoat < 0 || _lengthBoat > 50);
What's that semi colon doing there? (it's an empty statement).
kind regards,
Jos
Re: Need help with my while loops please do not know what I'm doing wrong
Thank you, that fixed it Jos. It is so strange how you miss the little things when looking at your own code. :)