Results 1 to 7 of 7
- 04-09-2009, 04:46 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
[SOLVED] Making Switch Case Insensitive?
I'm trying to make it so that when the user inputs a letter such as 'A' that it does not matter if it is upper or lower case. This is for a switch statement.
i want it so that when a user enters 'A' or 'a' (in the constructor) it wont matter, Alphadog will still print.Java Code:public Test(String toDO, char MovieLetter) { if (toDO.toLowerCase().startsWith("switch statement")) { P7Switch(MovieLetter); } else { System.out.println("Error: Please enter a valid request"); } } public void P7Switch(char movies) { switch(movies) { case 'A': System.out.println("Alphadog");; break; } }
Thank you anyone for help in advance!!
- 04-09-2009, 05:41 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
switch(movies)
{
case 'a':
case 'A': System.out.println("Alphadog");; break;
}
- 04-09-2009, 05:50 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You can take two ways to do this.
1. Simply you can use two cases like this.
Until you find the break statement all the cases are valid and execute. But seems it's odd. Think about if you want to use several characters.Java Code:switch(movies) { case 'A': case 'a': System.out.println("Text"); break; }
2. Did you know about equalsIgnoreCase() method on String class. You can make a logic depends on that too.
- 04-09-2009, 05:51 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-09-2009, 03:19 PM #5
... or you can convert the character to upper or lower case and not have to use the double switch:
Character toLowerCase method:Java Code:char myChar = Character.toLowerCase(Movie); switch(myChar) { case 'a':System.out.println("Alphadog"); break; }
Character toLowerCase method(Java Platform SE 6)
Character toUpperCase method:
Character toUpperCase method(Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-09-2009, 04:39 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 11
- Rep Power
- 0
thanks so much everyone!! figured it out with your help =]
- 04-09-2009, 04:46 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you solved the problem please mark the thread solved.
Similar Threads
-
String Title case
By bugger in forum New To JavaReplies: 6Last Post: 01-31-2012, 01:21 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 12:34 AM -
Switch Case and Key Events
By AndrewM16921 in forum New To JavaReplies: 4Last Post: 01-26-2009, 11:20 PM -
How can we write case insensitive queries for Xpath xml parsing
By vijayvcs in forum XMLReplies: 3Last Post: 09-11-2008, 03:00 AM -
Case Based Reasoning
By kbyrne in forum Advanced JavaReplies: 4Last Post: 04-12-2008, 08:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks