Results 1 to 2 of 2
  1. #1
    toby is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with Switch string code

    Hello, I have a question involving strings... Ill post the code then the question.
    Java Code:
    switch (jobChoice)
    		{
    			case 'p':
    				System.out.println("You are now a programer!");
    				setClass("programmer");
    				programmingStart();
    				break;
    			case 's':
    				System.out.println("You are now a soldier!");
    				setClass("soldier");
    				soldierStart();
    				break;
    			case 'm':
    				System.out.println("You are now working in a medical field");
    				setClass("medical");
    				medicalStart();
    				break;
    			default:
    				System.out.println("Not a valid choice.");
    				break;
    		}
    Java Code:
    public void setClass(String charClass)
    	{
    		classType = charClass;
    	}
    	
    	public void roll()
    	{
    		Random rand = new Random();
    		
    		if (classType.equals("programmer"))
    		{
    			strength = rand.nextInt(21) + 5;
    			dexterity = rand.nextInt(21) + 5;
    			constitution = rand.nextInt(21) + 5;
    			wisdom = rand.nextInt(21) + 5;
    			knowledge = rand.nextInt(21) + 5;
    		}
    The first code segment I set the classType to whatever the class is, but how do I use it in the class to determine which stats to roll? I've tried .equals a few different ways, as well as ==, maybe I'm messing up how the .equals works?
    Thanks.

  2. #2
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default

    You can only use a switch with Chars or Numbers so double, float, int. For strings you need to use if/elseif/else

    example
    Java Code:
    String myStr = "testing";
    if (myStr.equals("test"))
    {
    }
    else if (myStr.equals("tests"))
    {
    }
    else if (myStr.equals("testing"))
    {
    }
    else 
    {
    }
    Greetings.

Similar Threads

  1. Help with switch color
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 09-18-2008, 08:54 AM
  2. Method in a Switch Statement
    By cart1443 in forum New To Java
    Replies: 6
    Last Post: 03-14-2008, 04:48 AM
  3. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 AM
  4. String Code Indentation
    By andysh in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:45 AM
  5. Switch Statemet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-30-2007, 10:16 AM

Posting Permissions

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