Results 1 to 4 of 4
  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Exclamation java.util.NoSuchElementException

    Hello! Everytime i run my program i get the error java.util.NoSuchElementException.
    Here is my code

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class apples 
    {
    	static String read()
    	{
    		Scanner s = new Scanner(System.in);
    		String text = s.nextLine();
    		s.close();
    		return text;
    	}
    	
    	static void print(String t)
    	{
    		System.out.print(t);
    	}
    	static void println(String t)
    	{
    		System.out.println(t);
    	}
    	public static void main (String[] args) throws InterruptedException
    	{
    		Random rand = new Random();
    		int min,max;
    		println("Minimum number : ");
    		min = Integer.parseInt(read());
    		println("Maximum :");
    		max = Integer.parseInt(read());
    		println("How many?");
    		max = Integer.parseInt(read());
    		print("\nGenerating numbers between "+min+" and "+max+"!");
    		
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: java.util.NoSuchElementException

    When you start the program, the standard input stream for keyboard input is already open. But when you close the scanner it also closes the keyboard input stream. Subsequent calls to read will not reopen the standard input so you get an error. Also, creating a new scanner each time you want to read a line is not very efficient. I would recommend you just open the scanner at the beginning of your main program and use s.nextLine directly. You may also want to check out nextInt to avoid number format errors on input.

    Regards,
    Jim
    Last edited by jim829; 04-18-2013 at 10:39 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Unhappy Re: java.util.NoSuchElementException

    Quote Originally Posted by jim829 View Post
    I would recommend you just open the scanner at the beginning of your main program and use s.nextLine directly.
    i did that at first, but it doesn't recognise the scanner... D:

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: java.util.NoSuchElementException

    That's because you are declaring it as a local variable and opening it inside the read method. In fact, just get rid of the read method as it causes more problems than it's worth and wouldn't save you very much if it did work.

    Java Code:
    public static void main (String[] args) throws InterruptedException    {
        Scanner s = new Scanner(System.in);
        Random rand = new Random();
        int min,max;
        println("Minimum number : ");
        min = Integer.parseInt(s.nextLine());
            
    // rest of code here.
             
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. java.util.NoSuchElementException
    By tabishis in forum New To Java
    Replies: 1
    Last Post: 10-12-2012, 04:30 AM
  2. java.util.NoSuchElementException
    By Parkuss in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 07:33 AM
  3. Replies: 6
    Last Post: 03-11-2011, 03:53 AM
  4. Replies: 5
    Last Post: 02-27-2009, 05:19 AM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •