1

I am using Java to make an Uno game. I have a method called findCard(String cardname) whose function is to find the card in a hand of cards when the user writes in the name of the card (e.g: “Red 6”) and to return null if it can’t find the card. It works fine when I tried something like:

String card = "Red" + " " + "6";
pHand.findCard(card); // return the card Red 6

However, in the game, I will need the user to write a full command such as “deal Red 6”. Thus, I use StringTokenizer to separate the card’s name from the command:

StringTokenizer scan = new StringTokenizer(input);
String cmd = scan.nextToken(); // = "deal"
String color = scan.nextToken(); // = "Red"
String card = color + " " + scan.nextToken(); // = "Red 6"

What is wrong is when I try pHand.findCard(card); in this scenario, it only returns null no matter what was typed in. All I know about StringTokenizer is that it can split words in a string so I don't see how these are different. Thus, it would be great if anyone can point out the reason and the solution for this.

  • 4
    color + scan.nextToken() will result in "Red6" (notice no space). Try color + " " + scan.nextToken(). – Anton Kahwaji Nov 26 '19 at 00:30
  • [String.split()](https://docs.oracle.com/javase/9/docs/api/java/lang/String.html#split-java.lang.String-) might be a lot easier to manage. – MarsAtomic Nov 26 '19 at 00:36
  • Ah, I missed the space when I wrote this, but in my code, I had the white space between ``color`` and ``scan.nextToken()`` and it also did not work. – Jeon Soo Hye Nov 26 '19 at 06:50

1 Answers1

0

The comments already have the solutions really... but to wrap some more advice around it:

Your Problem

You're just missing a space between the color and number.

General Advice

  1. Make sure everything is the same case before comparing (e.g. all lower/upper).
  2. Get rid of white space when handling tokens.
  3. Keep variables separate; card value and card color are both unique and useful, putting them back in one string makes them less useful and harder to use and can introduce errors like yours.

Recommendation

Modify findCard() to take 2 strings, one with the card value and one with the color. Also, if you're using "red 6" as a key in a map or something, either:

  1. Make sure you build "red 6" from "red" and "6" using a function that you use everywhere you do this. That way you can unit test the function and you're sure that you don't add a space in one spot and forget it in another spot. Generally anywhere where you duplicate code, you might mess up - so don't duplicate.
  2. Make "card" a class and override equals and hash code to use both of these values when determining equality. Then you can use card as the key in a map fine.

Also, I kind of agree with the person who said that string spit() would be easier (just because you see it more often in code). But both should work if you're comfortable; so do it your way if you're happy :).

Community
  • 1
  • 1
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • I'm sorry I missed that white space when I wrote the question. In the code, I included it, but the method did not work either :( But your recommendations are really helpful, so I will try them out. Yeah, I agree that I can use ``split()``, but ``StringTokenizer`` is what my professor wants me to use so... :D – Jeon Soo Hye Nov 26 '19 at 07:01