我需要一些重要的帮助。我不知道该去哪儿。我完全没有深度了。
我需要从输入文件中读取一张牌号列表中的格式排名。
有效军衔为: 2-10,J,Q,K,A(小丑,王后,国王,Ace)
有效套间为: C,D,H,S(球杆,钻石,红心,黑桃)
然后,我需要将它们输出到一个文件中,以便输入文件具有2-c,然后程序将写入输出文件:
两个俱乐部-价值=2
我几乎已经选择了输入文件(我将在下面发布代码),但我不知道该做什么。我想我需要使用.nextline()来阅读每一行,但我不知道如何做到这一点。有人能帮忙吗?
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();
}发布于 2014-12-10 01:46:56
你可以采用传统的String#replace方式。下面的例子。
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();
}输入:
[2-C]输出:
2 of C - Value = 2您可以并且应该根据您的喜好修改println函数。
发布于 2014-12-10 01:46:04
这里有许多关于如何读取文件的问题/答案。为了解决你的整个问题,我会把它分解成以下步骤。此外,大多数技术细节可以通过在因特网上搜索来解决。。
String elements[] = line.substring(1,line.length()-1).split("-").rank,第二个元素为suit。如果您遵循上述五个步骤,这是解决问题的一个很好的路线图。
当你陷入困境时,就在谷歌上搜索一下,例如:
最后的想法:
给一个人一条鱼,你就喂他一天;教一个人钓鱼,你就给他一辈子的食物。
-Maimonides
https://stackoverflow.com/questions/27391971
复制相似问题