views:

89

answers:

4

Hello, I have an assignment of making a blackjack like program in a class. My first problem I am dealing with is creating an array of the cards. The professor wants an array setup with a txt file with the following format.

2 of hearts
2 of diamonds
2 of spades
2 of clubs
3 of hearts
3 of diamonds
3 of spades 

This goes on till face cards when it replaces the number with jack, queen, king, ace. Following the professors requirements, How would I take input from the txt file and just store the number and the hearts,diamonds,spades, and clubs. Thank you for the help

A: 

I'm sure you heard about the Scanner class.

But in case you haven't: http://java.sun.com/javase/6/docs/api/java/util/Scanner.html

klez
But in case you haven't: http://java.sun.com/javase/6/docs/api/java/util/Scanner.html
Lord Torgamus
@Lord Torgamus: Yeah, right, hope you don't mind if I add it to my answer.
klez
@klez, not at all.
Lord Torgamus
perfect, thank you for the link.
If anyone is still following this... How can I do this array when I get face cards that say king,jack..ect. I do not know how to set something up that allows a string or an integer... Thanks
@user337465, well, A=1, J=11, Q=12, K=13, so you don't need to store them as strings
klez
+1  A: 

Read the file line by line which you can split into its parts using the ' of ' as the separator.

zaf
A: 

Use the java.util.Scanner class, read in the file line by line, scan each line for the text 'of' to separate the card value from the card suit.

Kelly French
Since it's homework, I think the input file has a fixed format and cannot be changed.
klez
A: 

You can read lines with a Scanner object. Let's say your setup file is in "cards.txt"

Scanner sc = new Scanner(new File("cards.txt));

while(sc.hasNextLine()) {
    String line = sc.nextLine(); // each one of these will be like the "3 of Spades"
    // have code here to decode the line
}

This should point you in the right direction. Don't forget to import java.io.* (or .File) and java.util.* (or.Scanner)! :-)

glowcoder