-1

so I'm working on an activity and I need help on figuring out how will i get a string that inside a while loop. The code below is just a part of my activity so please disregard the doubles and char The code:

import java.util.Scanner;

public class ignCalcu {

    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        char operator;
        Double num1, num2, answer;
        System.out.println("Welcome to CalcuLegends!");
     while (true){
        System.out.println("Before you use the Calcore, would you like to set an IGN first?");
        System.out.println("Yes or No?");
        String start = input.nextLine();
        if (start.equals("Yes") || start.equals("yes")){
           System.out.println("What is your favorite color?");
           String color = input.nextLine(); 
           System.out.println("What word describes you the best?");
           String word = input.nextLine();
           System.out.println("What is your favorite animal?");
           String animal = input.nextLine();
         
           String ign = (" "+color+"_"+word+"_"+animal+" ");
           System.out.println("Hi" + ign + "! You are now using Calcore.");
           break;
        }
        System.out.println(ign);
       }
       }
       }

And here is the error:

source_file.java:26: error: cannot find symbol System.out.println(ign);

Symbol: variable ign

location: class ignCalcu 1 error

2 Answers2

0

ign variable is inside if block and so not visible outside if block when you do System.out.println().

You can declare variable ign outside if block

String ign = null;
if (start.equals("Yes") || start.equals("yes")){ {
    //Your other code
    ign = (" "+color+"_"+word+"_"+animal+" ");
}
Digsb
  • 320
  • 2
  • 8
0

You declared String ign inside your if statement, which means it will be only available within that if statement (this is its scope).

You are trying to use the ign variable outside the if statement and as a consequence you get the error. You can fix this by declaring the variable ign outside the if statement as follows:

public class ignCalcu {
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        char operator;
        Double num1, num2, answer;
        System.out.println("Welcome to CalcuLegends!");
        while (true){
            System.out.println("Before you use the Calcore, would you like to set an IGN first?");
            System.out.println("Yes or No?");
            String start = input.nextLine();
            String ign = "";
            if (start.equals("Yes") || start.equals("yes")){
                System.out.println("What is your favorite color?");
                String color = input.nextLine();
                System.out.println("What word describes you the best?");
                String word = input.nextLine();
                System.out.println("What is your favorite animal?");
                String animal = input.nextLine();

                ign = (" "+color+"_"+word+"_"+animal+" ");
                System.out.println("Hi" + ign + "! You are now using Calcore.");
                break;
            }
            System.out.println(ign);
        }
    }
}
João Dias
  • 16,277
  • 6
  • 33
  • 45