Click here to Skip to main content
12,072,540 members (59,767 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
I'm trying to add it to this code

char input;
String state = in.next();
 input = state.charAt(0);
 do
 {
     System.out.println("Please enter a command type:");
 }while(!input);
Posted 4-Nov-12 12:15pm
Edited 4-Nov-12 12:35pm
CPallini374.9K
v2
Rate this: bad
 
good
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?
  Permalink  
Comments
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
 
good
Please Sign up or sign in to vote.

Solution 2

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

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 | Mobile
Web03 | 2.8.160208.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2016
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