Solved

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

Posted on 2004-08-15
14
276 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:coffee_bean
  • 7
  • 3
  • 2
  • +1
14 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 11804809
Yes - use toLowerCase

toCon = Character.toLowerCase(toCon);
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 11804818
do {
    //
}while (toCon != 'n' && toCon != 'y');
0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11804883
You may also use something like:

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11804900
>>You may also use something like ...

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

0
 
LVL 7

Expert Comment

by:JugglerW
ID: 11805118
Your'e right but the coding for y, n, Y, N is same in every encoding (beside EBCDIC perhaps) :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11805127
>>but the coding for y, n, Y, N is same in every encoding

It is *at the moment* yes ... ;-)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 11805933
do {
      ......
      ......
} while ("YyNn".indexOf(toCon)>=0);

That way you can easily add exctra options.

or

do {
      ......
      ......
} while ("YN".indexOf(Character.toUpperCase(toCon))>=0);
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:coffee_bean
ID: 11845812
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');

      
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11845911
I liked objects' one the best myself ;-)

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

How could both conditions be simultaneously true? ;-)
0
 

Author Comment

by:coffee_bean
ID: 11846131
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);'.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11846212
It seems rather strange that you should wish it to terminate when either 'y' or 'n' is entered. What is the functionality required here?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11846267
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);
0
 

Author Comment

by:coffee_bean
ID: 11846610
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 11846679
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now