Results 1 to 5 of 5
  1. #1
    OmegaNine is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Question 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.
    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
    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?

    Thanks in advance.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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();
    
    	}

  3. #3
    OmegaNine is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default 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
    
    }

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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!

    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
    
    }
    info - getTemp method:
    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

  5. #5
    OmegaNine is offline Member
    Join Date
    Jul 2012
    Posts
    4
    Rep Power
    0

    Default 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

  1. Replies: 5
    Last Post: 12-13-2011, 01:11 AM
  2. Simple Scanner Question (i hope)
    By elm101 in forum New To Java
    Replies: 1
    Last Post: 07-09-2011, 01:24 AM
  3. Simple question Hopefully, Scanner/file
    By drucey in forum New To Java
    Replies: 23
    Last Post: 10-25-2010, 02:55 PM
  4. scanner question
    By kira137 in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 04:09 PM
  5. Scanner class question
    By Rgfirefly24 in forum New To Java
    Replies: 5
    Last Post: 04-25-2008, 01:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •