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 6 of 6

Thread: Scanner problem

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Scanner problem

    Here are some excerpts of my code...

    import java.util.*;
    import java.io.*;
     
    public class Driver2{
     
    	private static String fileName;
    	private static CLLOfRiders list = new CLLOfRiders();
     
    	private static Scanner input = new Scanner(System.in);
     
    	public static void main (String args[])
    	{	
    		System.out.println("Welcome to Project 2.");
    		System.out.println("What is the name of the data file?");
     
    		fileName = input.nextLine();
     
    		openFile(fileName);
    		commands(); //ERROR HERE!!!!!!!!!!!!
    		writeFile(fileName);
     
    		input.close();
    	}
     
              ....
     
               public static void commands()
    	{	
    		boolean loop = true;
    		String command;
     
    		while (loop == true)
    		{
    			System.out.println("\nNext command");
    			command = input.nextLine(); //ERROR HERE!!!!!!!!!!!!!!!
     
    			if (command.equals("add rider"))
    			{
    				System.out.println("Name");
    				String name = input.nextLine();
     
    				addRider(name);
    			}
    			...
    		}
    	}

    I'm getting this error from Eclipse.

    java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at Driver2.commands(Driver2.java:64)
    at Driver2.main(Driver2.java:21)
    The lines where the error occurs are in commented with "ERROR HERE". Why am I getting this exception? I rewrote command() in a test program and, normally, the Scanner waits for the user to input data. Not here, though?
    Last edited by sentimentGX4; September 30th, 2012 at 05:59 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Scanner problem

    Yes, I would expect nextLine() to block until input becomes available ... but only if it were the keyboard (console) being scanned and not, say, a file.

    In your code you show input being initialised and, a while later, being used. But what happens in between? Specifically, what is openFile()? In fact it would be best if you posted something complete enough for others to compile and run for themselves. Such a SSCCE need not be long (ie it can have everything that isn't part of the problem removed).

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

    sentimentGX4 (September 30th, 2012)

  4. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Scanner problem

    Thanks for the input! I updated the file with the code with the openFile() method. That is a contiguous block of code right there. There wasn't a lot in between. I just opened a new Scanner for scanning file input and then closed it when finished.

    I'll see if I can replicate the problem without bringing in all the lengthy coding involved with other related .java files.
    Last edited by sentimentGX4; September 30th, 2012 at 06:02 PM.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Scanner problem

    I was hoping that openFile() would show the static input scanner being set to something else. But it appears not...

    Do any commands get processed before the runtime exception occurs? If so it could be something else within the while loop that is causing the problem - addRider() is a candidate, or any other method that is called within the while loop before the exception.

    One diagnostic test might be to print the value of input both at the start of the pogram and then again as the while loop executes.

    private static Scanner input = new Scanner(System.in);
     
    public static void main (String args[])
    {
        System.out.println("input=" + input);
     
        // etc
    }
     
    public static void commands()
    {	
        boolean loop = true;
        String command;
     
        while (loop == true)
        {
            System.out.println("input=" + input);
            // etc
        }
        // etc
    }

    ---

    Constructing a SSCCE is tedious, but you often find that it reveals the source of a problem. Ie the problem goes away when an apparently unrelated block of code is removed.

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Scanner problem

    I solved the problem! It was from some weird Scanner misshap by using another Scanner named "input" in another one of my classes and then closing it. Thank you so much! Next time, I'll remember to name my Scanners different names.

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Scanner problem

    I'm glad you've got it sorted out.

Similar Threads

  1. Scanner Problem
    By Syahdeini in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 9th, 2012, 08:37 PM
  2. Problem with Scanner
    By Rafal75 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 7th, 2012, 08:34 PM
  3. Scanner problem - need help
    By destructobob in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 23rd, 2011, 02:08 AM
  4. [SOLVED] Problem with Scanner ?
    By derekeverett in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 19th, 2010, 04:34 AM
  5. Simple scanner problem
    By Sinensis in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 20th, 2009, 09:01 PM