I am taking an introductory class in programming with Java, so please excuse my ignorance when it comes to programming concepts/terminology. Part of my current assignment is to read an integer number (corresponding to length, in inches) and to convert it to centimeters, printing the final converted number as a floating point value. However, I always get an error message when trying to run the project (in Eclipse) that says:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at project1.inches(project1.java:82)
at project1.main(project1.java:17)
I have looked over my code several times, but can't seem to find out what the problem is -- Can anybody help me figure out how to fix this? I will include a snippet of the source code below:
public static void inches() {
//Define object variables
int vinch;
float vcent;
System.out.println("\n*******************************************************\n");
System.out.println("Inch to Centimeter Conversion\n\n------------------------\n");
//Read length (inches)
System.out.println("Enter a length (in inches):");
Scanner scanInches = new Scanner(System.in);
vinch = scanInches.nextInt();
//Convert input from inches to centimeters & print as float value
vcent = (float) (vinch * 2.84);
System.out.println(vinch + " inches is equal to " + vcent + " centimeters.");
scanInches.close();
}
Thanks for your help and once again, I apologize for such a trivial question!
**Note: I'm sure my code has errors or impracticalities in it...feel free to point these out, should you find any.
System.in
Scanner
? I don't