PDA

View Full Version : [Java] Help with string's


punx
10-19-2005, 04:56 PM
Hello once again, I have a question involving strings... Ill post the code then the qeustion.

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;
}



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 hwo the .equals works? Thanks for any help.

punx
10-19-2005, 04:59 PM
On another note, is it possible to work with a switch...case with strings? Thanks once again.

punx
10-19-2005, 05:34 PM
Solved the first problem. Never instantiated the object :o . Still have the other one though. Switch...Case.

gsoft
10-20-2005, 11:38 AM
You can only use a switch with Chars or Numbers so double, float, int. For strings you need to use if/elseif/else

example


String myStr = "testing";
if (myStr.equals("test"))
{
}
else if (myStr.equals("tests"))
{
}
else if (myStr.equals("testing"))
{
}
else
{
}

Aradon
10-20-2005, 07:39 PM
As far as the difference between .equals and ==

http://www.jguru.com/faq/view.jsp?EID=13549

punx
10-21-2005, 10:24 PM
Thanks guys, it was alot of help. :-D