Search code examples
variablesif-statementcompiler-errorssurvey

Error in the last part of code that is exactly the same as the rest of the code


I'm getting a variable initiation error in the last part of my code. I find it odd because it is no different in format than the rest of the code. I reviewed it several times and I'm just not seeing the problem. I think I need another set of eyes on this.

Ideas anyone?

 import java.util.Scanner;
 public class Survey
 {
public static void main(String[] args) 
{
  int surveyChoice;
  String choice;
  String choice2;
  String choice3;
  String choice4;
  final int FIRST_CHOICE = 1;
  final int SECOND_CHOICE = 2;
  final int THIRD_CHOICE = 3;
  final int FOURTH_CHOICE = 4;
  final int FIFTH_CHOICE = 5;
  final int SIXTH_CHOICE = 6;
  final int SEVENTH_CHOICE = 7;
  final int EIGHTH_CHOICE = 8;
  final String FIRST_PICK = "Love";
  final String SECOND_PICK = "Money";
  final String THIRD_PICK = "Long Safe Life";
  final String FOURTH_PICK = "Short Fun Life";
  final String FIFTH_PICK = "Few Very Close and Trusted Friends";
  final String SIXTH_PICK = "Internationaly Fameous";
  final String SEVENTH_PICK = "Pass away comfortably while sleeping";
  final String EIGHTH_PICK = "Quick Sudden Death doing what you love";
  String message;
  String message2;
  String message3;
  String message4; 

  Scanner input = new Scanner(System.in);
  System.out.println("What would you rather have?");
  System.out.println("Enter 1 for Love or 2 for Money.");
  surveyChoice = input.nextInt();

  if(surveyChoice == 1)
  {
     choice = FIRST_PICK;
     message = "Love";
  }
  else if(surveyChoice == 2)
  {
     choice = SECOND_PICK;   
     message = "Money"; 
  }
  else 
     choice = "invalid";
     message = "An invalid option";

  System.out.println("How would you rather live?");
  System.out.println("Enter 3 for Long Safe Life or 4 for Short Fun Life.");
  surveyChoice = input.nextInt();

  if(surveyChoice == 3)
  {
     choice2 = THIRD_PICK;
     message2 = "Long Safe Life";
  }
  else if(surveyChoice == 4)
  {
     choice2 = FOURTH_PICK;   
     message2 = "Short Fun Life"; 
  }
  else 
     choice2 = "invalid";
     message2 = "An invalid option";


  System.out.println("What kind of relationships would you rather have?");
  System.out.println("Enter 5 for Few Very Close and Trusted Friends or 6 for Internationaly Fameous.");
  surveyChoice = input.nextInt();

  if(surveyChoice == 5)
  {
     choice3 = FIFTH_PICK;
     message3 = "Few Very Close and Trusted Friends";
  }
  else if(surveyChoice == 6)
  {
     choice3 = SIXTH_PICK;   
     message3 = "Internationaly Fameous"; 
  }
  else 
     choice3 = "invalid";
     message3 = "An invalid option";


  System.out.println("How would you rather die?");
  System.out.println("Enter 7 for Pass away comfortably while sleeping or 8 for Quick Sudden Death doing what you love.");
  surveyChoice = input.nextInt();  

  if(surveyChoice == 7)
  {
     choice4 = SEVENTH_PICK;
     message4 = "Pass away comfortably while sleeping";
  }
  else if(surveyChoice == 8)
  {
     choice4 = EIGHTH_PICK;   
     message4 = "Quick Sudden Death doing what you love"; 
  }
  else 
     choice = "invalid";
     message = "An invalid option";

  System.out.println("You want " + message);
  System.out.println("You want " + message2);
  System.out.println("You want " + message3);
  System.out.println("You want " + message4);   
  System.out.println("You will have " + choice);
  System.out.println("You will have " + choice2);
  System.out.println("You will have " + choice3);
  System.out.println("You will have " + choice4);      
}

 }

Solution

  • As @OpiesDad pointed out, your variable names in the final block do not match.

    In addition, the first thing that popped out to me, was that you are missing brackets around the last else block in each if-then-else block, so that the appropriate message variable will always be set to "An invalid option".

    The last else should look like this:

    else 
    {
        choice = "invalid";
        message = "An invalid option";
    }