0

I need some major help. I have no idea where to go. I'm completely out of my depth.

I need to read in a list of playing card codes from an input file in the format [RANK - SUITE].

Valid ranks are: 2-10, J, Q, K, A (Joker, Queen, King, Ace)

Valid suites are: C, D, H, S (Clubs, Diamonds, Hearts, Spades)

Then I need to output them to a file, so the input file would have 2-c and then the program would write to the output file:

Two of Clubs - Value = 2

I've pretty much gotten as far as selecting the input file (i'll post the code below), but I have no idea what to do. I'm thinking I need to use .nextline() to read each line but I don't know how to do that. Can someone help?

 public class CardTest {
 Scanner input = new Scanner(System.in);

 public static void main(String[] args) 
 {
  inputFile(); 
 }

 public static void inputFile ()
 {
   JFileChooser chooser = new JFileChooser();
   Scanner in = null; 
   Scanner console; 

   try {
   if (chooser.showOpenDialog(null) == JFileChooser.CANCEL_OPTION)
   {
       System.exit(0); 
   }
   if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
   {
       File selectedFile = chooser.getSelectedFile(); 
       in = new Scanner(selectedFile); 

   }
   }
   catch (FileNotFoundException e) 
   {
       JOptionPane.showMessageDialog(null, "Could not find the file, please type the file name into dialog box");
       System.out.println("Type in your input file"); 
       console = new Scanner(System.in);
       String selectedFile = console.next(); 
       in = new Scanner(selectedFile); 

   }
   in.close(); 
 }
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
2redgoose
  • 73
  • 1
  • 3
  • 10

2 Answers2

1

You could go with the traditional String#replace way. Example below.

File file = ...;
try {
    Scanner scanner = new Scanner(file);
    while(scanner.hasNextLine()) {
        String line = scanner.nextLine();
        String[] values = line.split("-");
        String rank = values[0].replace("[", "").trim();
        String suite = values[1].replace("]", "").trim();
        System.out.println(rank + " of " + suite + " - Value = " + rank); // *****
    }
} catch (Exception e) {
    e.printStackTrace();
}

Input:

[2-C]

Output:

2 of C - Value = 2

You can and should modify that println function to your liking.

Constant
  • 780
  • 1
  • 5
  • 19
  • Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. Additionally notice he needs to convert numerical 2 to "two" and C to "clubs". – Menelaos Dec 10 '14 at 01:54
  • 1
    "Light a man a fire and he will be warm for a while. Set a man on fire and he will be warm for the rest of his life" – Matt Coubrough Dec 10 '14 at 01:57
  • Easily possible, again with the #replace method that I have introduced to him. – Constant Dec 10 '14 at 01:58
  • So I'd need to make a new replace method to do that then? I cannot modify what you gave me, correct? – 2redgoose Dec 10 '14 at 03:15
  • Figured it out. I just used "if" statements like if (rank.equals("2")) { rank = "Two"; value = "2"; } I'm sure it's not the most efficient way, but like I said, I don't plan on doing this for a living. Just want to get this project handed in. Thanks for your help. – 2redgoose Dec 10 '14 at 05:20
0

There are many questions/answers here on how to read files. In order to solve your whole problem I would break it down to the following steps. Additionally, most of the technical details can be solved by searching on the internet.

  1. Reading a file line by line. See: https://stackoverflow.com/a/4716623/1688441
  2. You need to parse each line. If you follow link in 1, logically each line will be a String. You could use regular expressions or you could just use the split method in order to split your string into an array of 2 elements. Use the '-' character. Example: String elements[] = line.substring(1,line.length()-1).split("-").
  3. So now you would have an array of 2 elements with 1st element being rank and second element being suit.
  4. You need to have a lookup table (e.g. using a HashMap) that converts the short abbreviations to actual textual descriptions. This map would probably be a class variable (C global equivalent) and you should initialize it on beginning.
  5. Make a method that takes the 2 element array, converts it to a textual descriptive statement, and writes it to a file.

If you follow the above 5 steps it's a good roadmap to solve your problem.

Do a search on google whenever you get stuck, for example:

Final thought:

Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.

-Maimonides

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • Thanks for your help. As for your quote, I'm not considering a career in computer science, I'm afraid I'm not smart enough for it. This is a GE class I'm taking and I clearly underestimated the difficulty level. I'll use the advice you gave me and continue to keep trying. – 2redgoose Dec 10 '14 at 02:02