Click here to Skip to main content
Sign Up to vote bad
good
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:15
Edited 4 Nov '12 - 11:35
CPallini246K


2 solutions

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:38
im trying to make it loop if the input is not within the range of a-g
CPallini - 4 Nov '12 - 17:46
then replace while (!input) with while (input < 'a' || input > 'g').
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
Your Filters
Interested
Ignored
     
0 OriginalGriff 216
1 Sergey Alexandrovich Kryukov 169
2 Tadit Dash 154
3 Richard MacCutchan 145
4 Santhosh G_ 115
0 Sergey Alexandrovich Kryukov 10,338
1 OriginalGriff 7,965
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,159


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 7 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid