Results 1 to 5 of 5
- 07-26-2012, 12:26 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Question about NoSuchElementException thrown from scanner.next().charAt(0)
I have searched around but not found an answer. It may be due to my inexperience and not understanding how it is applied to my situation. If so I apologies in advance.
I am trying to pull the first char of a user entered string using java.util.Scanner. When I enter a char and press enter at run time I get "java.util.NoSuchElementException" in the line "type = input.next().charAt(0);". I have tried both .next() and .nextLine()
The method contains the error is below. It is a practice assignment in a book, and my teacher can't give me an answer so I thought I would ask here.
From what I can see 'NoSuchElementException" is saying that the charAt(0) is trying to pull from a null object, but I have used this method of user input before with no problem. I am confused. Anyone see my newbie mistake?Java Code:private static char GetTempType(){ //Setting up vars and Scanner for input char type = ' '; boolean notSet = true; Scanner input = new Scanner(System.in); //This looks will keep going until the user enters a valid entry. while(notSet){ System.out.print("Would you like to convers to (C)elsius to (F)ahrenheit?"); type = input.next().charAt(0); if(type == 'C' || type == 'c'){ type = 'C'; notSet = false; }else if(type == 'F' || type == 'f'){ type = 'F'; notSet = false; }else{ System.out.println("Not a valid entry."); } }//End While loop input.close(); return type; }//end GetTempType
Thanks in advance.
- 07-26-2012, 12:45 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Question about NoSuchElementException thrown from scanner.next().charAt(0)
Can you give us a small runnable example with main method and so on? The code above runs fine here....
Java Code:public static void main(String... args) throws Exception { char type = ' '; boolean notSet = true; Scanner input = new Scanner(System.in); // This looks will keep going until the user enters a valid entry. while (notSet) { System.out .print("Would you like to convers to (C)elsius to (F)ahrenheit?"); type = input.next().charAt(0); if (type == 'C' || type == 'c') { type = 'C'; notSet = false; } else if (type == 'F' || type == 'f') { type = 'F'; notSet = false; } else { System.out.println("Not a valid entry."); } }// End While loop input.close(); }
- 07-26-2012, 12:49 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: Question about NoSuchElementException thrown from scanner.next().charAt(0)
Here is the full program. The problem occurs in the first method called, its a big paste, but mostly just filler for the problem.
Java Code:package temp; import java.util.Scanner; public class Temp { public static void main(String[] args) { char type = ' '; double userTemp = 0; double convertedTemp = 0; type = GetTempType(); userTemp = GetTemp(type); switch (type){ case 'F': convertedTemp = ConvertCtoF(userTemp); break; case 'C': convertedTemp = ConvertFtoC(userTemp); break; } DisplayResults(convertedTemp, type); }//End main //Parameters: double - temp // char - temp type //Returns: void private static void DisplayResults(double temp, char type){ if(type == 'F'){ System.out.println("The temperature in Farenheit: " + temp); }else if(type == 'C'){ System.out.println("The temerature in Celcius: " + temp); } }//ENd DisplayResults //Parameters: double - Farenheit temp //Returns: double - Celcius temp private static double ConvertCtoF(double tempC){ double tempF = tempC * 1.8 + 32; return tempF; }//end ConvertCtoF //Parameters: double - Celsius temp //Returns: double - Farenheit temp private static double ConvertFtoC(double tempF){ double tempC = (tempF - 32) / 1.8; return tempC; }//end ConvertFtoC //Parameters: char - F or C (For types of temps) //Returns: double - Entered Temp private static double GetTemp(char type){ double temp = 0; Scanner input = new Scanner(System.in); if(type == 'C'){ System.out.print("Please enter the temperature in Fahrenheit: "); temp = input.nextDouble(); }else if(type == 'F'){ System.out.print("Please enter the temperature in Celcius: "); temp = input.nextDouble(); } input.close(); return temp; }//End GetTemp //prompts and reads a char from user, then returns a cleaned version private static char GetTempType(){ char type = ' '; boolean notSet = true; Scanner input = new Scanner(System.in); while(notSet){ System.out.print("Would you like to convers to (C)elsius to (F)ahrenheit?"); type = input.next().charAt(0); if(type == 'C' || type == 'c'){ type = 'C'; notSet = false; }else if(type == 'F' || type == 'f'){ type = 'F'; notSet = false; }else{ System.out.println("Not a valid entry."); } }//End While loop input.close(); return type; }//end GetTempType }
- 07-26-2012, 01:13 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Question about NoSuchElementException thrown from scanner.next().charAt(0)
Ok, it seems that the second Scanner uses internal the same input/stream (probably due to the buffering) which the first Scanner object already has consumed ?!
I would advise you to use only one scanner object and close this scanner once at the end!
info - getTemp method:Java Code:package temp; import java.util.Scanner; public class Temp { private static Scanner input = new Scanner(System.in); public static void main(String[] args) { char type = ' '; double userTemp = 0; double convertedTemp = 0; type = GetTempType(); userTemp = GetTemp(type); input.close(); switch (type) { case 'F': convertedTemp = ConvertCtoF(userTemp); break; case 'C': convertedTemp = ConvertFtoC(userTemp); break; } DisplayResults(convertedTemp, type); }// End main // Parameters: double - temp // char - temp type // Returns: void private static void DisplayResults(double temp, char type) { if (type == 'F') { System.out.println("The temperature in Farenheit: " + temp); } else if (type == 'C') { System.out.println("The temerature in Celcius: " + temp); } }// ENd DisplayResults // Parameters: double - Farenheit temp // Returns: double - Celcius temp private static double ConvertCtoF(double tempC) { double tempF = tempC * 1.8 + 32; return tempF; }// end ConvertCtoF // Parameters: double - Celsius temp // Returns: double - Farenheit temp private static double ConvertFtoC(double tempF) { double tempC = (tempF - 32) / 1.8; return tempC; }// end ConvertFtoC // Parameters: char - F or C (For types of temps) // Returns: double - Entered Temp private synchronized static double GetTemp(char type) { double temp = 0; if (type == 'C') { System.out.print("Please enter the temperature in Fahrenheit: "); temp = input.nextDouble(); } else if (type == 'F') { System.out.print("Please enter the temperature in Celcius: "); temp = input.nextDouble(); } return temp; }// End GetTemp // prompts and reads a char from user, then returns a cleaned version private synchronized static char GetTempType() { char type = ' '; boolean notSet = true; while (notSet) { System.out .print("Would you like to convers to (C)elsius to (F)ahrenheit?"); type = input.next().charAt(0); if (type == 'C' || type == 'c') { type = 'C'; notSet = false; } else if (type == 'F' || type == 'f') { type = 'F'; notSet = false; } else { System.out.println("Not a valid entry."); } }// End While loop return type; }// end GetTempType }
if(type == 'C'){
System.out.print("Please enter the temperature in Fahrenheit: ");
are you sure that this is correct? :)
and please take a look at the Java Code/naming Conventions
- 07-26-2012, 01:36 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 4
- Rep Power
- 0
Re: Question about NoSuchElementException thrown from scanner.next().charAt(0)
The way its worded is kind of funny, but to the user it is fine. If they are choosing C they are wanting to convert to C from F. Its still being put together, I ran in this problem and the teacher had no idea what was wrong. I rewrote the program last night and still got the same error so I cam here.
Thanks for the naming cheat sheet. Its been forever since I took a java class. This is actually a LAN class, but he gave me these assignments as a "refresher" for our final project.
It looks like moving it to a class scope works perfectly. Thank you for your help, I will be sure to tell my teacher where I got the help ;)
Similar Threads
-
Help with NoSuchElementException, relates to Scanner class
By WhiteRaven6 in forum New To JavaReplies: 5Last Post: 12-13-2011, 12:11 AM -
Simple Scanner Question (i hope)
By elm101 in forum New To JavaReplies: 1Last Post: 07-09-2011, 12:24 AM -
Simple question Hopefully, Scanner/file
By drucey in forum New To JavaReplies: 23Last Post: 10-25-2010, 01:55 PM -
scanner question
By kira137 in forum New To JavaReplies: 8Last Post: 10-12-2009, 03:09 PM -
Scanner class question
By Rgfirefly24 in forum New To JavaReplies: 5Last Post: 04-25-2008, 12:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks