[英]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.
此外,大多数技术细节都可以通过在互联网上搜索来解决。
String elements[] = line.substring(1,line.length()-1).split("-")
. String elements[] = line.substring(1,line.length()-1).split("-")
。 rank
and second element being suit
. rank
,第二个元素为suit
。 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.