I am brand new to java this semester. So far it has been going well, but having trouble figuring out how to do this one part of a program I am working on. This program requires the user to input a playing card, abbreviated, such as 9D for 9 of Diamonds, AC for Ace of Clubs and so on. It reads a 2-3 character string from the user, and then using if statements outputs the value and suit of the playing card. For instance, the user inputs "JS", the program should output "Jack of Spades" and terminate. The if statements are not the issue I am having, but my instructor wants us to have it input as a string, then break apart the string input by the user, and use those pieces as conditions for the if statements. The problem I am coming into is how to break apart the string when the first part of the string can be either numbers or characters. Here is a snippet of the program I have written so far.
I am just stuck on how to break apart the string basically.

NOTE: DO NOT SEEK ANSWER GIVEN TO ME, JUST IDEA OF WHAT I SHOULD BE THINKING ABOUT TO GET TO THE CORRECT CODE!

import java.util.*;

public class CardConverter 
{

	public static void main(String[] args) 
	{
			//Declare variables and set up input for user.
		Scanner keyboard = new Scanner(System.in);
		String input;
		int valueNumber;
		char valueChar;
		char suit;
		
			//Get input from the user using nextLine command.
		System.out.println("Input the abbreviated card, first the number or face value, then the suit: ");
			input = keyboard.nextLine();
			valueNumber = keyboard.nextInt();
			
			
		
			
	}

}

Thanks in advance.

Recommended Answers

All 2 Replies

Well, you know that the rank will be 1 or 2 characters. The suit will always be a single character at the end. Using the substring() and length() methods of String, you should be able to separate the string easily into the two portions that you need.

Ok, got the program written, seperating the string out, using if statements to produce output, but something is wrong. I get no warnings or errors and it compiles fine, but when i run it, no matter what input i use, i get no ouput. I cannot figure this out for the life of me. Here is my code. By the way, the directions from my instructor were that it is assumed that the correct input is known by the user, so that is why I do not have any bad input if statements in this program.

//Program that reads a 2-3 character string and outputs the full description of the playing card.

import java.util.*;

public class CardConverter 
{

	public static void main(String[] args) 
	{
			//Declare variables and set up input for user.
		Scanner keyboard = new Scanner(System.in);
		String input;		//Variable to store input of the user input string.
		int valueNumber;	//Variable if value of card is numerical.
		char valueChar;		//Variable if value of card is alphabetical.
		char suit;			//Variable for the suit from input.
		int x;				//Variable for the length of input string.
		
			//Get input from the user using nextLine command.
		System.out.println("Input the abbreviated card, first the number or face value, then the suit: ");
			input = keyboard.nextLine();
			x = input.length();
		
			
			
			if (x == 2)
			{
				valueNumber = keyboard.nextInt();
				suit = input.charAt(2);
					System.out.print("Ten ");	
					System.out.print("of ");
					
					if (suit == 'S')
						System.out.println("Spades");
					if (suit == 'D')
						System.out.println("Diamonds");
					if (suit == 'C')
						System.out.println("Clubs");
					if (suit == 'H')
						System.out.println("Hearts");
			}
			
			if (x == 1)
			{
				valueNumber = keyboard.nextInt();
				valueChar = input.charAt(0);
				suit = input.charAt(1);
				
					if (valueNumber == 9)
						System.out.print("Nine ");
					if (valueNumber == 8)
						System.out.print("Eight ");
					if (valueNumber == 7)
						System.out.print("Seven ");
					if (valueNumber == 6)
						System.out.print("Six ");
					if (valueNumber == 5)
						System.out.print("Five ");
					if (valueNumber == 4)
						System.out.print("Four ");
					if (valueNumber == 3)
						System.out.print("Three ");
					if (valueNumber == 2)
						System.out.print("Two  ");
					if (valueChar == 'J')
						System.out.print("Jack ");
					if (valueChar == 'Q')
						System.out.print("Queen ");
					if (valueChar == 'K')
						System.out.print("King ");
					if (valueChar == 'A')
						System.out.print("Ace ");
					
					System.out.print("of ");
					
					if (suit == 'S')
						System.out.println("Spades");
					if (suit == 'D')
						System.out.println("Diamonds");
					if (suit == 'C')
						System.out.println("Clubs");
					if (suit == 'H')
						System.out.println("Hearts");
					
			}
		}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.