Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can anyone tell me why this isnt working?

    The code should ask for the color then test to see if it is equal to "red" or "blue". It reads it in fine. But when it comes to the testing it doesnt work. I even printed out what it is reading in and it is equal to "red" but it bypasses that if statement. can anyone tell me why?

    Code:
    public static void main(String[] args) {
         String color = promptString("Enter the color for the ball (red or blue): ");
         String ballColor = color;
        System.out.println(color);
         if((ballColor.equals("red"))){
           Color myColor = Color.RED;
         }
         if((ballColor.equals("blue"))){
           Color myColor = Color.BLUE;
         }
           else{
           System.out.println(color + " is not valid, pelase try again!");
    color = promptString("Enter the color for the ball (red or blue): ");
         if((color.equals("red"))){
           Color myColor = Color.RED;
         }
         if((color.equals("blue"))){
           Color myColor = Color.BLUE;
         }
         else{
           throw new IllegalArgumentException(color + " is not valid. goodbye!");
    
              public static String promptString(String prompt) {
          System.out.print(prompt);
          return CONSOLE.nextLine();
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    It'll be a little bit easier if its formatted more consistently; I'll run this through my formatting tool:
    PHP Code:
        public static void main(String[] args)
        {
            
    String color promptString("Enter the color for the ball (red or blue): ");
            
    String ballColor color;
            
    System.out.println(color);
            if ((
    ballColor.equals("red")))
            {
                
    Color myColor Color.RED;
            }
            if ((
    ballColor.equals("blue")))
            {
                
    Color myColor Color.BLUE;
            }
            else
            {
                
    System.out.println(color " is not valid, pelase try again!");
                
    color promptString("Enter the color for the ball (red or blue): ");
                if ((
    color.equals("red")))
                {
                    
    Color myColor Color.RED;
                }
                if ((
    color.equals("blue")))
                {
                    
    Color myColor Color.BLUE;
                }
                else
                {
                    throw new 
    IllegalArgumentException(color
                            
    " is not valid. goodbye!");
                }
            }
        }

        public static 
    String promptString(String prompt)
        {
            
    System.out.print(prompt);
            return 
    CONSOLE.nextLine();
        } 
    Note I added three closing braces to the main method; the current was incomplete.

    The problem is your if/else[if] nesting. With the formatting above, you should be able to trace it a little easier; in particular, follow what happens when color is not equal to "blue". Hint: you're overcomplicating it here; you probably want a single do/while and a single if/elseif branch.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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