0

I'm trying to get input from the console a second time within the program using Scanners but when calling a second Scanner in another method it’s coming up with NoSuchElement exception.

If I remove startMenu() from running fileReader() it works, however for some reason after running it throws the exception.

    public class Garden {
    public static final Garden GARDEN = new Garden();
    //variable declartaions removed
    public static void main(String[] args) {
        if (null != args && 0 < args.length) {
            GARDEN.fileName = args[0].trim();
        }
        if (GARDEN.fileName != null) {
            GARDEN.fileReader(GARDEN.fileName);
        } else {
            GARDEN.fileReader();
        }

        GARDEN.startMenu();
        int mainI = 0;
        while (mainI != 1000000) {
            try {
                Thread.sleep(0);
            } catch (InterruptedException e) {
            }
            GARDEN.daysWeather();
            GARDEN.anotherDay();
            mainI++;
        }
    }


    protected void fileReader() { // asks for file name for config file
        System.out.println("Enter File Name Please");
        Scanner cmdReader = null;
        String cmdInput = null;
        try {
            cmdReader = new Scanner(System.in);
            cmdInput = cmdReader.nextLine();
        } catch (NoSuchElementException noSuchElement) {
            noSuchElement.printStackTrace();
            fileReader();  //throwing error here
        }

        //code removed
    }



    protected void startMenu() {// modified code from ATM lab (week2)
    int selected = 0;
        //code removed 
        Scanner climateScanner = new Scanner(System.in);
        System.out.println("Select a number 1-4");
        selected = climateScanner.nextInt();
        switch (selected) {
        case 1: // semiarid
            weatherType = 10; //10% chance to rain
            climateScanner.close();
            break;
        case 2: // arid
            weatherType = 20; //5% chance to rain
            climateScanner.close();
            break;
        case 3:
            weatherType = 50; //2% chance to rain
            tropicalWeather = true;
            climateScanner.close(); 
            break;
        case 4:
            weatherType = 20;//5% chance to rain 
            storming = true;
            climateScanner.close();
            break;
        default:
            System.out.println("Invalid Input try again");
            startMenu(); //using Recursion to ask for input again
            break;
        }
        //code removed
    }
}
user1642671
  • 85
  • 2
  • 11
  • GARDEN is the singleton created didn't think this is the problem as it works with GARDEN.fileReader() when GARDEN.startMenu() isnt called – user1642671 Dec 11 '13 at 11:26

2 Answers2

1
 GARDEN.startMenu();// method id not a static one.

you can't access that in this way. you have to initialize the class or make your method static. and also what is GARDEN ??

Ok now you are edited your code.

Again

 GARDEN.fileReader(GARDEN.fileName); // you are parsing input argument 
                                // But method in your class is no argument method
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • Or make `startMenu` *static*. – Maroun Dec 11 '13 at 11:23
  • fileReader(GARDEN.fileName) is just another instance of fileReader() that instead of asking for a fileName takes it from when the program is started in console java Garden testA.txt will run fileReader(fileName) java Garden will run fileReader() – user1642671 Dec 11 '13 at 11:29
  • the difference between the two fileReader() methods is that one that takes the parameter gets the fileName from the parameter while the one without a parameter gets it from console input via the scanner (not sure if thats what you mean by do i have another method in same name) – user1642671 Dec 11 '13 at 11:32
  • @user1642671 I suggest you to use `debuger` . Then easily you will realize the issue – Ruchira Gayan Ranaweera Dec 11 '13 at 11:34
  • I have tried it gets cmdReader = new Scanner(System.in); and then throws exception is there something i have to do with System.in after every use? – user1642671 Dec 11 '13 at 11:41
0

Problem was 2 different Scanner (one in startMenu() and one in readFile()) changed it so there was a Scanner scanner = new Scanner(System.in) in class variables and then calling scanner.nextLine() from within the method

user1642671
  • 85
  • 2
  • 11