Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Scanner reading from console via System.in causing NoSuchElementException?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Scanner reading from console via System.in causing NoSuchElementException?

    I made the program and it ran fine i then added a new method and that method somehow is causing errors in a different method and im unsure what is happening other than it appears to be to do with Scanner and System.in

     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();
            }
     
            int mainI = 0;
            while (mainI != 1000000) {
                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                }
                GARDEN.daysWeather();
                GARDEN.anotherDay();
                mainI++;
            }
        }
    	private Garden() {
    		System.out.println("_______________________________________");
    		startMenu();
    	}
     
        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
        }
    }

    The program still runs fine when I take out the startMenu(); Ive been trying to work this out for 2 hours so any help would be grealy appreciated.
    http://pastebin.com/2YRCk73E code for the entire program is there though theres a lot and i don't think it will all help in finding the issue (i copied the Garden 2x by accident)


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Scanner reading from console via System.in causing NoSuchElementException?

    Quote Originally Posted by Pipastrolo View Post
    I made the program and it ran fine i then added a new method and that method somehow is causing errors in a different method and im unsure what is happening other than it appears to be to do with Scanner and System.in
    Speaking very in general, if you read from standard-input (System.in) through Scanner, you should have, and use, only 1 Scanner object for this.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    Pipastrolo (December 11th, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Scanner reading from console via System.in causing NoSuchElementException?

    Thanks ive been trying to fix this for ages

Similar Threads

  1. Replies: 4
    Last Post: April 16th, 2013, 06:50 AM
  2. [SOLVED] NoSuchElementException(), simple program not behaving right with Scanner class.
    By mwebb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2011, 06:41 PM
  3. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  4. Console box with scanner?
    By Hallowed in forum Java Theory & Questions
    Replies: 1
    Last Post: May 26th, 2011, 12:50 AM
  5. Reading user input from the console with the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 11th, 2008, 02:47 PM

Tags for this Thread