简体   繁体   English

在卡片组中阅读

[英]Reading in card deck

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]. 我需要从输入文件中以[RANK-SUITE]格式读取纸牌代码列表。

Valid ranks are: 2-10, J, Q, K, A (Joker, Queen, King, Ace) 有效等级为:2-10,J,Q,K,A(小丑,皇后,国王,王牌)

Valid suites are: C, D, H, S (Clubs, Diamonds, Hearts, Spades) 有效套件包括:C,D,H,S(俱乐部,钻石,红心,黑桃)

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: 然后,我需要将它们输出到文件,因此输入文件将具有2-c,然后程序将写入输出文件:

Two of Clubs - Value = 2 两家具乐部-价值= 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. 我在想我需要使用.nextline()来读取每一行,但是我不知道该怎么做。 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(); 
 }

You could go with the traditional String#replace way. 您可以采用传统的String#replace方法。 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. 您可以并且应该根据自己的喜好修改该println函数。

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 参见: 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. 如果您遵循1中的链接,则逻辑上每一行都是一个字符串。 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. 您可以使用正则表达式,也可以只使用split方法将字符串拆分为2个元素的数组。 Use the '-' character. 使用“-”字符。 Example: String elements[] = line.substring(1,line.length()-1).split("-") . 示例: 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 . 因此,现在您将拥有一个由2个元素组成的数组,其中第一个元素为rank ,第二个元素为suit
  4. You need to have a lookup table (eg using a HashMap) that converts the short abbreviations to actual textual descriptions. 您需要一个查找表(例如,使用HashMap)将简短的缩写转换为实际的文字描述。 This map would probably be a class variable (C global equivalent) and you should initialize it on beginning. 该映射可能是一个类变量(C全局等效项),您应该在开始时对其进行初始化。
  5. Make a method that takes the 2 element array, converts it to a textual descriptive statement, and writes it to a file. 创建一个采用2元素数组的方法,将其转换为文本描述性语句,然后将其写入文件。

If you follow the above 5 steps it's a good roadmap to solve your problem. 如果您按照上述5个步骤进行操作,这是解决您的问题的理想路线图。

Do a search on google whenever you get stuck, for example: 遇到困难时,请在Google上进行搜索,例如:

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 -Maimonides

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM