Results 1 to 6 of 6
- 09-17-2008, 05:23 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
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
-
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?
- 09-17-2008, 05:53 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
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.
- 09-17-2008, 06:50 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
-
I think that it's time to talk to your teacher; that's what I think
- 09-17-2008, 07:29 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 3
- Rep Power
- 0
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?
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
-
Exception in thread "main" java.lang.NoClassDefFoundError: client Help
By b000m in forum New To JavaReplies: 6Last Post: 08-18-2008, 06:44 AM -
Exception in thread "main" java.util.NoSuchElementException
By ragav in forum New To JavaReplies: 4Last Post: 06-08-2008, 02:19 PM -
[SOLVED] Exception in thread "main" java.util.NoSuchElementException
By thevoice in forum New To JavaReplies: 5Last Post: 05-14-2008, 01:43 PM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks