-2

I'm not 100% sure what is wrong, it just tells me that blue has not been initialized.

Millionaire m1 = new Millionaire(response);
//Q1
 System.out.println("What color is blue?");
   System.out.println("1. blue");
   System.out.println("2. red");
   System.out.println("3. turquoise");
   System.out.println("4. What is 'color'?");
 input.nextLine();
 String ans = input.nextLine();     
 String blue;

 if(ans == blue){
   System.out.println("correct!");
   m1.addMoney();
 }else{
 System.out.println("Lost on the first question??");
 System.exit(0);   
}   
  • 1
    Well, in this case you literally never assign any value to `blue`. Why do you think you have? – markspace Mar 14 '21 at 23:28
  • 1
    `I'm not 100% sure what is wrong,` No it's 100% correct. Can you show us *where* you think `blue` is initialized? – markspace Mar 14 '21 at 23:29
  • I thought I did with 'String blue;', but I guess I didn't. – Suggapoppa Wills Mar 14 '21 at 23:30
  • 1
    And don't compare String with == please, use equals instead. – Dren Mar 14 '21 at 23:32
  • More fundamentally, you seem to be confusing the name of a variable with its value. The value of the variable 'ans' may be the string "blue". This has no relationship to a variable that happens to have the name 'blue'. You don't need that variable at all; you should write `ans.equals("blue")`. – user15187356 Mar 15 '21 at 02:49

1 Answers1

0

It looks like blue isn't initialised anywhere, writing

String blue;

only declares a variable and doesn't set it to anything. Therefore when you're doing your comparison

ans == blue

you're comparing ans to nothing, or more specifically null.


Also as @Typhon said in the comments, you shouldn't use == to compare the value of two objects, but for reference equality. I would take a look here for some more information.

Henry Twist
  • 5,666
  • 3
  • 19
  • 44