Results 1 to 7 of 7
  1. #1
    iPetey is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Question [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.

    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;
         
            }
        }
    i want it so that when a user enters 'A' or 'a' (in the constructor) it wont matter, Alphadog will still print.

    Thank you anyone for help in advance!!

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    switch(movies)
    {
    case 'a':
    case 'A': System.out.println("Alphadog");; break;

    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You can take two ways to do this.

    1. Simply you can use two cases like this.

    Java Code:
    switch(movies) {
         case 'A':
         case 'a':
               System.out.println("Text");
               break;
    }
    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.

    2. Did you know about equalsIgnoreCase() method on String class. You can make a logic depends on that too.


  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by mtyoung View Post
    switch(movies)
    {
    case 'a':
    case 'A': System.out.println("Alphadog");; break;

    }
    Oops, post the same thing lol. I didn't see your post. Sorry about that.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    ... or you can convert the character to upper or lower case and not have to use the double switch:
    Java Code:
    char myChar = Character.toLowerCase(Movie);
    switch(myChar)
    {
      case 'a':System.out.println("Alphadog"); break;
    }
    Character toLowerCase method:
    Character toLowerCase method(Java Platform SE 6)
    Character toUpperCase method:
    Character toUpperCase method(Java Platform SE 6)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    iPetey is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    thanks so much everyone!! figured it out with your help =]

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. String Title case
    By bugger in forum New To Java
    Replies: 6
    Last Post: 01-31-2012, 02:21 PM
  2. Replies: 2
    Last Post: 02-23-2009, 01:34 AM
  3. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-27-2009, 12:20 AM
  4. Replies: 3
    Last Post: 09-11-2008, 04:00 AM
  5. Case Based Reasoning
    By kbyrne in forum Advanced Java
    Replies: 4
    Last Post: 04-12-2008, 09:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •