Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By wsaryada
  • 2 Post By JosAH

Thread: Need help with my while loops please do not know what I'm doing wrong

  1. #1
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default 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:
    Java 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

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default 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.

    Java 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.
    Last edited by wsaryada; 12-20-2012 at 07:35 AM.
    ndsmith20 likes this.

  3. #3
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default 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?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default 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
    wsaryada and ndsmith20 like this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default 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:
    Java 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:

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Need help with my while loops please do not know what I'm doing wrong

    Quote Originally Posted by ndsmith20 View Post
    Java Code:
    		while (_lengthBoat < 0 || _lengthBoat > 50);
    What's that semi colon doing there? (it's an empty statement).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ndsmith20 is offline Member
    Join Date
    Oct 2012
    Posts
    55
    Rep Power
    0

    Default 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. :)

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Help with while loops
    By enjoy2225 in forum New To Java
    Replies: 9
    Last Post: 10-26-2012, 12:27 AM
  3. I can't find anything wrong with this but somehow it's wrong.
    By Biscuit Tickler in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 09:28 PM
  4. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  5. While Loops, need a bit of help.
    By Keno777 in forum New To Java
    Replies: 7
    Last Post: 10-30-2009, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •