Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

If statements, strings, not getting the right answer  RSS feed

 
Sandra Edwards-Thro
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am supposed to be writing a program that takes card notation (4S) and gives you the description (4 of Spades). Here is the Card class:



And here is the code for the CardTester:

When I enter "4S" I receive "Invalid Entry" instead of the expected "4 of Spades". Any hints on what's going wrong? This is my first time using if statements, so I'm sure I'm messing something up ( ;
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see two problems.

1) This doesn't do what it looks like you think it does. It returns the original String, not the first character.


2) You aren't assigning values to

and the like. This means you are comparing "S" to null in your if statement. Which is never true so it falls through to the invalid case.
 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Jeanne is correct.
Also for make use of switch statement for better coding standards instead of too many if/else statements.

Thanks,
Ashwini Kashyap
 
Campbell Ritchie
Marshal
Posts: 55694
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a few months you will know how to use enumerated types to store the suits, and Maps to store a link between the name and the suit. Go through the Enum class and the Java Language specification and see whether there is anything else, eg, being able to get enum elements from their name.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have way to much code to just start finding problems now. NEVER write more than 2-5 lines of code before compiling and testing. With your 60 lines of code, how do you know where to START looking for problems?

The idea behind programming is that you can divide the problem up into teeny tiny parts. you code one at a time, and only when you are sure it works, you move on. For example, every time I start something new, I write only enough to have my program print "Hello fred". Once that works, I may write the code that gets the input from the user and echoes it back out. Once I know I can GET the card notation from the user, I'd write the code to validate it. Then to break it apart into the rank and suit.

I keep going like that, testing at each step of the way. I end up writing and deleting a LOT of System.out.println() statements, but at least I am confident at each stage that my code works.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:In a few months you will know how to use enumerated types to store the suits...

But until then, you can still create a Suit class.

Tip (and it's a biggie): DON'T use Strings as substitutes for other types. A Suit is a type with a specific meaning, and not just the String "Spade" or "Diamond"; and a Rank is quite a bit more than just the number (or letter) on a Card.

Try and think how you might set them up as classes, and then incorporate those into your Card class.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!