Adam Basham
Adam Basham

Reputation: 21

While Not Equal to String

I'm making a text based RPG game and I am creating a chooseClass method. See the code:

public static void classChoice(){
    String cont =null;
    String[] classes = {"rogue", "wizard", "knight", "archer"};
    Scanner input = new Scanner(System.in);
    do{
        System.out.println("Choose your class (Rogue, Wizard, Knight, Archer): ");
        cont = input.next();
        if (cont.equalsIgnoreCase("rogue")){
            System.out.println("You have chosen the Rogue!");
        } else if (cont.equalsIgnoreCase("wizard")) {
            System.out.println("You have chosen the Wizard!");
        } else if (cont.equalsIgnoreCase("knight")) {
            System.out.println("You have chosen the Knight!");
        } else if (cont.equalsIgnoreCase("archer")) {
            System.out.println("You have chosen the Archer!");
        } else {
            System.out.println("Choose a valid class!");
        } 
    } while(!cont.equals(classes));
}

So I made a string array for all of the classes, and I thought that I could make the user input "cont" and say that while it is not equal to any of the "classes" array values, then print the message "Choose a valid class!". It isn't working, any ideas?

Upvotes: 2

Views: 8826

Answers (8)

Bohemian
Bohemian

Reputation: 424983

I don't know if you actually need the array, but let's assume you do:

String[] classes = {"rogue", "wizard", "knight", "archer"};
List<String> list = Arrays.asList(classes);
    Scanner input = new Scanner(System.in);
do (
    System.out.println("Choose your class (Rogue, Wizard, Knight, Archer): ");
    cont = input.next();
} while (!list.contains(cont.toLowerCase()))

You can refine it, but this is the basics of something that will work.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718758

Among other things, you have a syntax error:

if (cont.equalsIgnoreCase("rogue")){
    System.out.println("You have chosen the Rogue!");
}   // <<< -- syntax error
}else if(cont.equalsIgnoreCase("wizard")){

If you indented your code properly that error would be much easier to see.

Upvotes: 1

C. Ross
C. Ross

Reputation: 31848

cont is a String and classes is a String[] (String array), so they will never be equal. What you want to know is if classes contains cont. I would suggest changing classes to a List<String> so you can use the contains method.

final List<String> CLASSES = Arrays.asList("rogue", "wizard", "knight", "archer");

And then

while(!CLASSES.contains(cont));

That said, your variable names and general design could use some work. For starters cont should probably be named something more descriptive like userClassChoice.

Upvotes: 2

rybosome
rybosome

Reputation: 5126

You can't compare a single String to an array of Strings. The correct way to think of this logic is, "does the set of valid classes contain whatever the user typed in?". A small snippet of what that might look like is as follows:

Set<String> classes = new HashSet<String>(Arrays.asList("Rogue", "Wizard")); // ...etc.
while (!classes.contains(cont));

Upvotes: 2

Crickcoder
Crickcoder

Reputation: 2155

The statement in while block is incorrect. You shouldn't compare con with classes array

Upvotes: 1

user2357112
user2357112

Reputation: 280291

cont.equals(classes) doesn't test whether cont is in classes. It tests whether cont is equal to classes. Since cont is a String and classes is an array of Strings, this will never be true.

I recommend using Arrays.asList to make a list of classes. Then, you can test whether the list contains cont:

classes = Arrays.asList("rogue", "wizard", "knight", "archer");

... while (!classes.contains(cont));

Upvotes: 10

Charaf JRA
Charaf JRA

Reputation: 8334

this will check if cont value is in your array :

 while (!classes.asList("rogue", "wizard", "knight", "archer").contains(cont))

You try to compare a string cont with an array classes.

Upvotes: 1

tbodt
tbodt

Reputation: 16987

You should use an enum for this purpose. it would have a method to return an enum for an input string, and then you could find out what the value was.

Upvotes: 4

Related Questions