Results 1 to 6 of 6
  1. #1
    vileoxidation is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Question Exception in thread "main" java.util.NoSuchElementException

    I am relatively new to Java, and am writing my programs in Eclipse. I am having somewhat of a strange issue, because my code is compiling fine in Eclipse, but when I submit it (its for a class, and we are using online submission that compiles and runs sample data when you submit) I get an exception. I have no idea what the error means, but here is the entire text of it:


    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:838)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextDouble(Scanner.java:2387)
    at Assignment2.main(Assignment2.java:71)


    Can anyone tell me what could be going on here? Any help would be much appreciated! My source code is below.

    Java Code:
    import java.util.*;
    import java.text.*;
    
    /*
    *******************************************************
    DESCRIPTION: This is a program that takes the distance of 
    three javelin throws that were entered by the user (in meters),
    converts them to yards, feet, inches, and centimeters, and displays the output. 
    *******************************************************
    */
    
    public class Assignment2 
    {
    	  public static void main (String[] args)
    	   {
    		  // Variable declarations
    		  final double M_TO_Y = 1.0936;
    		  final double M_TO_C = 100;
    		  final double M_TO_F = 3.28084;
    		  final double M_TO_I = 39.370;
    		  // double distance1;
    		  //double distance2; 
    		  //double distance3;
    		  
    		  // Set up for the scanners
    		  Scanner competitor = new Scanner(System.in);
    		  Scanner user = new Scanner(System.in);
    		  Scanner dist1 = new Scanner(System.in);
    		  Scanner dist2 = new Scanner(System.in);
    		  Scanner dist3 = new Scanner(System.in);
    		  
    		  // Prompt for competitor's name
    		  System.out.print("Enter the competitor's name: ");		  
    		  String cName = competitor.nextLine();
    		  
    		  // Heading for throws
    		  System.out.println("\n\t\tOlympic Javelin Throws");
    		  
    		  // Prompts for the three distances
    		  System.out.println("\n\tEnter the distances, in meters, for " 
    				  + cName + ":");
    		  System.out.print("\nPlease enter the distance for throw 1: ");
    		  	double distance1 = dist1.nextDouble();
    		  System.out.print("Please enter the distance for throw 2: ");
    		  	double distance2 = dist2.nextDouble();
    		  System.out.print("Please enter the distance for throw 3: ");
    		  	double distance3 = dist3.nextDouble();
    				  
    		  // Heading for conversion chart
    		  System.out.println("\n\tyards\t\tfeet\t\tinches\t\tcm");
    		  
    		  // Decimal formatting set-up
    		  DecimalFormat fmt = new DecimalFormat ("0.##");
    		  
    		  // Conversions for distance1
    		  System.out.print("\t" + fmt.format(distance1 * M_TO_Y));
    		  System.out.print("\t\t" + fmt.format(distance1 * M_TO_F));
    		  System.out.print("\t\t" + fmt.format(distance1 * M_TO_I));
    		  System.out.println("\t\t" + fmt.format(distance1 * M_TO_C));
    		  
    		  // Conversions for distance2
    		  System.out.print("\t" + fmt.format(distance2 * M_TO_Y));
    		  System.out.print("\t\t" + fmt.format(distance2 * M_TO_F));
    		  System.out.print("\t\t" + fmt.format(distance2 * M_TO_I));
    		  System.out.println("\t\t" + fmt.format(distance2 * M_TO_C));
    		 
    		  // Conversions for distance3
    		  System.out.print("\t" + fmt.format(distance3 * M_TO_Y));
    		  System.out.print("\t\t" + fmt.format(distance3 * M_TO_F));
    		  System.out.print("\t\t" + fmt.format(distance3 * M_TO_I));
    		  System.out.println("\t\t" + fmt.format(distance3 * M_TO_C));	
    		  
    	   } // End of main method
    	} // End of Assignment2 class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Did you read the Scanner API and see when, where, and why it throws this exception?
    2) How are you getting the "sample data" into the program? That's key here. Are you sure that System.in will work the same when you compile and run your code via online submission?

  3. #3
    vileoxidation is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    Thank you for the reply!

    Where is the Scanner API? As I said, I am new to this, so I really don't know what that is. I have seen, in google searches, that it happens because the "input is exhausted," but I don't know what that actually means.

    Well, I don't exactly know how the online submission works. I just submit the file, and then the screen in the screenshot below comes up.
    Attached Thumbnails Attached Thumbnails Exception in thread "main" java.util.NoSuchElementException-java-screen-shot.jpg  

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    1) Did you read the Scanner API and see when, where, and why it throws this exception?
    This is interesting. First of all try to find what that exception exactly means. Basically it indicates that there are no more elements in enumeration. So the best thing is to check the data source.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that it's time to talk to your teacher; that's what I think

  6. #6
    vileoxidation is offline Member
    Join Date
    Sep 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    This is interesting. First of all try to find what that exception exactly means. Basically it indicates that there are no more elements in enumeration. So the best thing is to check the data source.
    I am trying to understand the error, but all I can pull out (and this is just a guess) is that there is something wrong between how my program is handling the data and the data itself. I think that is also what "the input is exhausted" means, but I don't know how to change the program to work with the data.

    Is it a problem with the "nextDouble" term and the input? Should "nextDouble" be something else, or is there something wrong with my scanner, or something entirely different?

    Quote Originally Posted by Fubarable View Post
    I think that it's time to talk to your teacher; that's what I think
    Well, that is my next option. I just wanted to try to figure it out first, and understand the exception, especially since I am unsure exactly how much help my teacher will be willing to provide.

    Thank you for the help. I will just get in contact with my teacher. :)

Similar Threads

  1. Replies: 6
    Last Post: 08-18-2008, 07:44 AM
  2. Replies: 4
    Last Post: 06-08-2008, 03:19 PM
  3. Replies: 5
    Last Post: 05-14-2008, 02:43 PM
  4. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  5. Replies: 1
    Last Post: 07-25-2007, 11:55 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
  •