Link to home
Start Free TrialLog in
Avatar of coffee_bean
coffee_bean

asked on

Character class. Shorten code for checking upper and lower case.

Can anyone help advice on the following?

Is there any way to shorten the condtion for checking upper and lower case?

Given that the loop accept only 'Y', 'y', 'N' and 'n' for yes and no?

do {
      ......
      ......
} while ((toCon!='Y') && (toCon! ='y') && (toCon!='N') && (toCon!= 'n'));

Do any of the these methods come into use for solving the above-mentioned qn?

Character.isLowerCase(chr)
Character.isUpperCase(chr)
Character.toLowerCase(chr)
Character.toUpperCase(chr)
Character.isLetter(chr)

Thanks in advance.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JugglerW
JugglerW

You may also use something like:

    public static boolean isYNyn( char ch )    
    {
        ch &= ~32;        
        return ch == 'Y' || ch == 'N';
    }

>>You may also use something like ...

Although it's generally not too good to be assumptive about character encodings

Your'e right but the coding for y, n, Y, N is same in every encoding (beside EBCDIC perhaps) :-)
>>but the coding for y, n, Y, N is same in every encoding

It is *at the moment* yes ... ;-)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coffee_bean

ASKER

Thanks CEHJ for the reply.

I tested out the codes on simple stt below and it worked if I enter Y or y or N or n to terminate a loop.

But I couldn't get it to work the other way round if it's for continuing a loop using while (toCon == 'n' && toCon == 'y');

-----------------------------------
char toCon;
char option;

do {
      System.out.println("End loop? ");
      option = in.readLine().charAt(0);
      toCon = Character.toLowerCase(option);
}while (toCon != 'n' && toCon != 'y');

      
I liked objects' one the best myself ;-)

>>while (toCon == 'n' && toCon == 'y');

How could both conditions be simultaneously true? ;-)
objects, Thanks for the answer.

I tested out ur codes and it works well if it's for continuing loop.

char toCon;
do {
     System.out.println("To loop? ");
     toCon = in.readLine().charAt(0);
}while ("YN".indexOf(Character.toUpperCase(toCon))>=0);

Couldn't get it terminated ("End loop?") if Y or y or N or n  is entered when  condition is changed to !=. Similarly case for 'while ("YyNn".indexOf(toCon)>=0);' .

I assume this code read the first char of letter entered. It will only terminate when Y is entered to a 'while ("YyNn".indexOf(toCon) !=0);'.
It seems rather strange that you should wish it to terminate when either 'y' or 'n' is entered. What is the functionality required here?
A normal usage pattern would be:

do {
      // do something, then ask:
      System.out.print("Do you want to stop now? (y\n) ");
      char toCon = in.readLine().charAt(0);
} while("Yy".indexOf(toCon) < 0);
Well, I am new to Java. A beginner learner.

I was given this qn without any stt provided in the do while loop.  I was kind of obscured with trying to shorten the condtion stt while getting it to work (loop). I didn't notice the conflicting condition until much later.  So I treat the lower & upper case Y & N as any other letter to get the stt to loop or otherwise.

Really appreciate all the answers. It has helped me learn something on the Char class method which I won't be able on my own or from my classmates.