-1

I am trying to use a number (1/2/3) the user inputs to control something. When I try to use converted choice it says cannot find symbol. Why is this?

// Start user input //
    public static int userChoice() {
        Scanner userInput = new Scanner(System.in);
        String choice = userInput.nextLine();
        int convertedChoice = Integer.parseInt(choice);
        return convertedChoice;
    }
    // End user input //

    // Start scan maze //
    static void whatMaze() {
        if (convertedChoice == 1) {
            System.out.println("you chose 1");
        } else {
            System.out.println("you chose something else");
        }
    }
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 6
    You've defined `convertedChoice` inside the `userChoice()` method, you can't use it from `whatMaze()`. – Kayaman Nov 24 '15 at 13:51
  • I thought me setting userChoice to public and returning convertedChoice would allow me to do this? – jord_turansky Nov 24 '15 at 13:52
  • userChoice is a method, so it being public will only allow you to call it from anywhere. You either need to return your value from that method or put all of these methods in a class with a choice attribute. I recommend the former. – abalos Nov 24 '15 at 13:53
  • 3
    Possible duplicate of [What is the difference between a local variable, an instance field, an input parameter, and a class field?](http://stackoverflow.com/questions/20671008/what-is-the-difference-between-a-local-variable-an-instance-field-an-input-par) – Yassin Hajaj Nov 24 '15 at 13:54
  • thanks @abalos I now get the method is public and not the variable. In terms of returning the value in the method is that not what "return convertedChoice;" does? Thanks – jord_turansky Nov 24 '15 at 13:54
  • You need to update your whatMaze() function to include the return value from userChoice(). See my answer below. – abalos Nov 24 '15 at 18:04

5 Answers5

1

You must call userChoice(), and use the output, since the variable convertedChoice is only in scope (declared) in the method.

e.g.

 if (userChoice() == 1) {
       System.out.println("you chose 1");
 } else {
      System.out.println("you chose something else");
 }

You could declare convertedChoice as a member in your class, but I wouldn't do that since in more complex scenarios it leads you open to shared-state/threading problems etc.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

convertedChoice is local to userChoice meaning you can't access it outside that method.

You probably meant to call userChoice to use the returned value:

if (userChoice() == 1) {
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
0

Give the value of convertedChoice as parameter to your function

// Start user input //
    public static int userChoice() {
        Scanner userInput = new Scanner(System.in);
        String choice = userInput.nextLine();
        int convertedChoice = Integer.parseInt(choice);
        return convertedChoice;
    }
    // End user input //

    // Start scan maze //
    static void whatMaze(int convertedChoice) {
        if (convertedChoice == 1) {
            System.out.println("you chose 1");
        } else {
            System.out.println("you chose something else");
        }
    }

In your main function (or wherever you use it):

whatMaze(userChoice());
Simon Tenbeitel
  • 835
  • 1
  • 8
  • 20
0
import java.util.Scanner;

public class Example {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          int choice = userChoice();
          whatMaze(choice);
    }


    // Start user input //
    public static int userChoice() {
        Scanner userInput = new Scanner(System.in);
        String choice = userInput.nextLine();
        int convertedChoice = Integer.parseInt(choice);
        return convertedChoice;
    }
    // End user input //

    // Start scan maze //
    static void whatMaze(int convertedChoice) {
        if (convertedChoice == 1) {
            System.out.println("you chose 1");
        } else {
            System.out.println("you chose something else");
        }
    }

}
Zach P
  • 560
  • 10
  • 30
frank
  • 1
0
// Start user input //
    public static int userChoice() {
        Scanner userInput = new Scanner(System.in);
        String choice = userInput.nextLine();
        int convertedChoice = Integer.parseInt(choice);
        return convertedChoice;
    }
    // End user input //

    // Start scan maze //
    static void whatMaze() {
        if (userChoice() == 1) {
            System.out.println("you chose 1");
        } else {
            System.out.println("you chose something else");
        }
    }
abalos
  • 1,301
  • 7
  • 12