Click here to Skip to main content
13,353,106 members (79,612 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm trying to add it to this code

char input;
String state =;
 input = state.charAt(0);
     System.out.println("Please enter a command type:");
Posted 4-Nov-12 12:15pm
Updated 4-Nov-12 12:35pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Nope, there is not. However, if the string input is empty then the charAt method throws the IndexOutOfBoundsException (see the documentation[^]).
What are you trying to do?
diego14567 4-Nov-12 17:38pm
im trying to make it loop if the input is not within the range of a-g
CPallini 4-Nov-12 17:46pm
then replace while (!input) with while (input < 'a' || input > 'g').
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

try this
Scanner in=new Scanner(;
		char ch ;
			System.out.println("Enter the a character");
			ch= in.nextLine().charAt(0);
			System.out.println("Char: "+ch);			
		catch (IndexOutOfBoundsException e) {
		      ch = 'z';
		}while(ch < 'a' || ch > 'g');

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180111.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100