Click here to Skip to main content
11,644,136 members (73,836 online)
Rate this: bad
good
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 11:15am
Edited 4-Nov-12 11:35am
CPallini348K
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 at 4-Nov-12 17:38pm
   
im trying to make it loop if the input is not within the range of a-g
CPallini at 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
0 OriginalGriff 610
1 jyo.net 484
2 DamithSL 318
3 Afzaal Ahmad Zeeshan 308
4 Sergey Alexandrovich Kryukov 246
0 OriginalGriff 795
1 DamithSL 611
2 Mika Wendelius 494
3 jyo.net 484
4 Afzaal Ahmad Zeeshan 404


Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2015
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