Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: (beginner) Switch Statement

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default (beginner) Switch Statement

    Hello, I would like to ask a question regarding a switch case of mine. I am new to Java(have dabbled in c++ a little though.)

    In one of my classes I have a method designed to take a character as a parameter and return a string based on the appropriate case.

    The problem is the string that is always being returned is the default "INVALID", could someone point me in the appropriate direction? I checked the java tutorial but it didn't offer me much help, is there a problem with comparing integer literals / character literals on the same switch test condition?

    private String GetNumber(char number)
    	{
    		String numberString = null;
    		if(Character.isLetter(number))
    		{
    			Character.toUpperCase(number);
     
    		}
    		switch(number)
    		{
    			case 'A': numberString = "Ace";
    				break;
    			case 2: numberString = "two";
    				break;
    			case 3: numberString = "three";
    				break;
    			case 4: numberString = "four";
    				break;
    			case 5: numberString = "five";
    				break;
    			case 6: numberString = "six";
    				break;
    			case 7: numberString = "seven";
    				break;
    			case 8: numberString = "eight";
    				break;
    			case 9: numberString = "nine";
    				break;
    			case 1: numberString = "ten";
    				break;
    			case 'J': numberString = "jack";
    				break;
    			case 'Q': numberString = "Queen";
    				break;
    			case 'K': numberString = "King";
    				break;
    			default : numberString = "INVALID";
    				break;
    		}
    		return numberString;
    	}


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: (beginner) Switch Statement

    One thing that jumps out is your case numbers. You have them as ints but your switch statement variable is a char. You numbers should be '2' or '3' etc...

    also you may have to take the input as a string and use the charAt() method.
    Last edited by mwr76; October 11th, 2011 at 10:31 PM.

  3. The Following User Says Thank You to mwr76 For This Useful Post:

    georgewbw (October 11th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: (beginner) Switch Statement

    Quote Originally Posted by mwr76 View Post
    One thing that jumps out is your case numbers. You have them as ints but your switch statement variable is a char. You numbers should be '2' or '3' etc...

    also you may have to take the input as a string and use the charAt() method.
    I actually did use the charAt method to get that value.

    Also the single quotes around my cases solved the problem. Thread marked as solved and a Thanks to you (;

Similar Threads

  1. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  2. Advancing day w/ if/else & switch statement
    By rbread80 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 10:43 PM
  3. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  4. How to Use the Java switch statement
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: October 8th, 2009, 09:00 PM
  5. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM