0

6.) Scenario – You are playing a guessing game with your little cousin. She wants you to guess her favorite color. Create a program using the While loop. Below is a code I'm writing but having trouble with the while loop.

package loopscenario;
import java.util.Scanner;
import java.util.Random;
/**
 *
 * @author macbook
 */
public class LoopScenario6 {
public static void main(String[] args)
{
 String blue = null;
 String yellow = null ;
 String red = null;
 String pink = "pink";
 String favoriteColor = null;

 Scanner keyboard = new Scanner(System.in);
 Random randomNumber = new Random();
 Random aRan = new Random();


 while (!favoriteColor.equalsIgnoreCase(pink))

     if (favoriteColor.equalsIgnoreCase(blue))
     {
         System.out.println("Wrong color. Please try again.");
     }
     else if( favoriteColor.equalsIgnoreCase(yellow))
     {
       System.out.println("Wrong color. Please try again.");  
     }
     else if ( favoriteColor.equalsIgnoreCase(red))
     {
      System.out.println("Wrong color. Please try again.");     
     }
    else 
     {
      System.out.println("Yes!");     
     }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Roberto Martinez
  • 13
  • 1
  • 1
  • 5
  • 1
    What specific trouble are you having with the while loop, and where? Editing your question to add this would improve the question and make it more answerable. – paisanco Oct 10 '15 at 01:57
  • Exception in thread "main" java.lang.NullPointerException at loopscenario.LoopScenario6.main(LoopScenario6.java:27 This is line 27 while (!favoriteColor.equalsIgnoreCase(pink)) – Roberto Martinez Oct 10 '15 at 02:00
  • Also, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Oct 10 '15 at 02:01
  • `String favoriteColor = null;` well crap, you never change this. – Hovercraft Full Of Eels Oct 10 '15 at 02:02
  • Thank you for your help. – Roberto Martinez Oct 10 '15 at 02:09
  • In the future, if you don't understand the error messages, search Google or this site for them as they've usually been asked about here **many** times. – Hovercraft Full Of Eels Oct 10 '15 at 02:14

0 Answers0