Click here to Skip to main content
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 12:15pm
Edited 4-Nov-12 12:35pm
CPallini337.4K
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 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web02 | 2.8.150331.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