0

Ok guys so i am trying to read in from 1 out of 3 files normally with out the if ... else clause

just Scanner myScanner = new Scanner(new File("maze1.txt")); would run the problem, but i want the user to select what maze he/she would like to run

public class Main {
    private static Scanner myScanner;

    public static void main(String[] args) throws FileNotFoundException {

        System.out.println("Please Enter 1,2 or 3 to pick the different maze you would like being solved.");
        Scanner myScannerinput = new Scanner(System.in);



        int Mazenumber = myScannerinput.nextInt();

        if(Mazenumber == 1){
            Scanner myScanner = new Scanner(new File("maze1.txt"));
        }
        else if(Mazenumber == 2){
            Scanner myScanner = new Scanner(new File("maze2.txt"));
        }
        else if(Mazenumber == 3){
            Scanner myScanner = new Scanner(new File("maze3.txt"));
        }
        else{
            System.out.println("You did not choose one of the 3 mazes");
        }



        int numRows = myScanner.nextInt();
        int numCols = myScanner.nextInt();
        myScanner.nextLine();

        int startX = 0;
        int startY = 0;'

Please Enter 1,2 or 3 to pick the different maze you would like being solved.

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:33)
6
  • The line myScanner.nextLine(); should be generating a compiler error, because myScanner's scope each time is only within the if and else if blocks. Commented Oct 4, 2013 at 23:59
  • Notice that you've declared myScanner as an automatic local 3 times, with none of those declarations accessible outside the if ladder. Commented Oct 5, 2013 at 0:15
  • @rgettman - It doesn't create a compile error because he's also declared myScanner as a static. Commented Oct 5, 2013 at 0:16
  • (You shouldn't make variables that are essentially local to a method "static". They should be declared inside the method.) Commented Oct 5, 2013 at 0:17
  • @HotLicks I didn't see the static variable myScanner before. Thanks for pointing that out. Commented Oct 5, 2013 at 0:20

3 Answers 3

1

don't create individual new Scanner inside the if condition. Create one Scanner before the if condition and assign the file based on your preference.

        int Mazenumber = myScannerinput.nextInt();
        Scanner myScanner =null;
        if(Mazenumber == 1){
            myScanner = new Scanner(new File("maze1.txt"));
        }
        else if(Mazenumber == 2){
            myScanner = new Scanner(new File("maze2.txt"));
        }
        else if(Mazenumber == 3){
            myScanner = new Scanner(new File("maze3.txt"));
        }
        else{
            System.out.println("You did not choose one of the 3 mazes");
        }
        if(myScanner!=null){
           int numRows = myScanner.nextInt();
           int numCols = myScanner.nextInt();
           myScanner.nextLine();
       }
Sign up to request clarification or add additional context in comments.

Comments

0

You define and initialise myScanner inside the if block, so it will be out of scope as soon as you leave it

2 Comments

His code compiles. The variable declared within the if-statements shadows another variable in the outer scope.
@arshajii The variable that the value is assigned to gets out of scope, whatever other variable (not shown in given code anyway) never gets a value.
0

Instead of

 int Mazenumber = myScannerinput.nextInt();

        if(Mazenumber == 1){
            Scanner myScanner = new Scanner(new File("maze1.txt"));
        }
        else if(Mazenumber == 2){
            Scanner myScanner = new Scanner(new File("maze2.txt"));
        }
        else if(Mazenumber == 3){
            Scanner myScanner = new Scanner(new File("maze3.txt"));
        }
        else{
            System.out.println("You did not choose one of the 3 mazes");
        }

Use

Scanner myScanner;
int Mazenumber = 0;
do {
    System.out.println("Choose 1, 2 or 3");
    Mazenumber = myScannerinput.nextInt();
    if(Mazenumber >=1 && Mazenumber <=3) {
         String fileName = "maze"+Mazenumber+".txt";
        myScanner = new Scanner(new File(fileName));
    } else {
           System.out.println("You did not choose one of the 3 mazes");      
     }

} while(Mazenumber>3 && Mazenumber <1);

Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:33)

The exception is because, you've declared Scanner myScanner inside if-else block.

4 Comments

That won't compile. You can only do the URL trick if some statement or block comes after it. Besides, let's not confuse the OP with that...
@arshajii OP wants with out the if ... else clause. I hope it'll compile. What's the problem with this?
int Mazenumber = myScannerinput.nextInt(); String fileName = "maze"+Mazenumber+".txt"; Scanner myScanner = new Scanner(new File(fileName));
@ClearMist It is a good practice in stackoverflow to close the question if you've got your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.