Computer Science Canada

New at java need help!!

Author:  magicman [ Fri Oct 01, 2004 11:29 am ]
Post subject:  New at java need help!!

I'm making a word game based off of DnD, and its not working. What happens is when you pic a race, the stats are supose to pop up, but it doesnt.
Heres my code:

code:


// The "DungeonsAndDragons" class.
import java.awt.*;
import hsa.Console;

public class DungeonsAndDragons
{
    static Console c;           // The output console

    public static void main (String [] args)
    {
        c = new Console ();
        String classes;
        String name;
        String race;
        int str = 10;
        int dex = 10;
        int Int = 10;
        int wis = 10;
        int con = 10;
        int cha = 10;
        c.println ("Whats your name?? <First Name Only>");
        name = c.readString ();
        c.clear ();

        c.println ("What race do you want to be?");
        c.println ("human");
       // c.println ("dwarf");
        //c.println ("elf");
        //c.println ("gnome");
        //c.println ("half-elf");
        //c.println ("half-orc");
        //c.println ("halfling");
        race = c.readString ();

        if (race == "human")
        {
            c.println ("Here are the human's stats:");
            c.println ("strength:" + str);
            c.println ("dexterity:" + dex);
            c.println ("intelligence:" + Int);
            c.println ("wisdom:" + wis);
            c.println ("constitution:" + con);
            c.println ("charism:" + cha);
        }

        // else if (race == "dwarf")
        //
        //     {
        //         c.println ("Here are the Dwarfs stats:");
        //         c.println ("strength:" + str);
        //         c.println ("dexterity:" + dex);
        //         c.println ("intelligence:" + Int);
        //         c.println ("wisdom:" + wis);
        //         c.println ("constitution:" + con + 2);
        //         c.println ("charism:" + (cha - 2));
        //     }
        //
        // else if (race == "elf")
        //
        //     {
        //         c.println ("Here are the Elfs stats:");
        //         c.println ("strength:" + str);
        //         c.println ("dexterity:" + dex + 2);
        //         c.println ("intelligence:" + Int);
        //         c.println ("wisdom:" + wis);
        //         c.println ("constitution:" + (con - 2));
        //         c.println ("charism:" + cha);
        //     }
        //
        // else if (race == "gnome")
        //
        //     {
        //         c.println ("Here are the gnome's stats:");
        //         c.println ("strength:" + (str - 2));
        //         c.println ("dexterity:" + dex);
        //         c.println ("intelligence:" + Int);
        //         c.println ("wisdom:" + wis);
        //         c.println ("constitution:" + con + 2);
        //         c.println ("charism:" + cha);
        //     }
        //
        // else if (race == "half-elf")
        //
        //     {
        //         c.println ("Here are the Half-Elf's stats:");
        //         c.println ("strength:" + str);
        //         c.println ("dexterity:" + dex);
        //         c.println ("intelligence:" + Int);
        //         c.println ("wisdom:" + wis);
        //         c.println ("constitution:" + con);
        //         c.println ("charism:" + cha);
        //     }
        //
        // else if (race == "half-orc")
        //
        //     {
        //         c.println ("Here are the Half-orc's stats:");
        //         c.println ("strength:" + str + 2);
        //         c.println ("dexterity:" + dex);
        //         c.println ("intelligence:" + (Int - 2));
        //         c.println ("wisdom:" + wis);
        //         c.println ("constitution:" + con);
        //         c.println ("charism:" + (cha - 2));
        //     }
        //
        // else if (race == "halfling")
        //
        //     {
        //         c.println ("Here are the halfling's stats:");
        //         c.println ("strength:" + (str - 2));
        //         c.println ("dexterity:" + dex + 2);
        //         c.println ("intelligence:" + Int);
        //         c.println ("wisdom:" + wis);
        //         c.println ("constitution:" + con);
        //         c.println ("charism:" + cha);
        //     }

        c.clear ();

        c.println ("What class do you want??");
        c.println ("Barbarian");
        c.println ("Bard");
        c.println ("Cleric");
        c.println ("Druid");
        c.println ("Fighter");
        c.println ("Monk");
        c.println ("Paladin");
        c.println ("Ranger");
        c.println ("Rogue");
        c.println ("Sorcerer");
        c.println ("Wizard");
        classes = c.readString ();
        c.clear ();





        //c.println ("Your name is, " + name);
        //c.println ("Your race is, " + race);
        //c.println ("Your class is, " + classes);

        // Place your program here.  'c' is the output console
    } // main method
} // DungeonsAndDragons class




please help

Author:  wtd [ Fri Oct 01, 2004 1:11 pm ]
Post subject: 

I'm not familiar with the proprietary Holt classes, but does the readString method of Console automatically chomp the newline off of input?

That could be the problem. Try testing:

code:
if (race == "human" || race == "human\n")

Author:  McKenzie [ Fri Oct 01, 2004 1:45 pm ]
Post subject: 

2 problems.
1. You can't compare strings like that in Java. try
if (race.equals("human"))
2. Your clear that comes next will clear it before you have a chance to read it

Author:  wtd [ Fri Oct 01, 2004 1:54 pm ]
Post subject: 

McKenzie wrote:
2 problems.
1. You can't compare strings like that in Java. try
if (race.equals("human"))


I feel like a moron for forgetting that.

I want Groovy! Smile

code:
switch (race) {
   case "human":
      // yada yada
}


: