【问题标题】:How do I separate these tokens and skip the lines when I need to?如何分隔这些标记并在需要时跳过这些行?
【发布时间】:2014-03-03 17:46:07
【问题描述】:

//我正在尝试获取这个文件并根据最后的######### 分离卡片 //我正在用java制作一个robocop风格的纸牌游戏来玩,我制作了纯文本文件中的卡片,我希望能够读取文件并将文件分成卡片。最终,我希望能够调用这些卡片并在需要时将它们打印出来。

试试{

                File file = new File("MyText.txt");
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line = null;
                while((line = reader.readLine()) != null){
                    addCard(line);
                }

            }catch(Exception ex){
                ex.printStackTrace();
            }
            //Problem! This only prints the first card?!? HELP! 
            for(i = 0; i < playerList.size(); i++){
                System.out.println(playerList.get(i));
            }

    }


    void addCard(String lineToParse){

        String[] tokens = lineToParse.split("##########");
        // How do I increment the token to the next token and add to playerList?
        playerList.add(tokens[0]);

    }
}

【问题讨论】:

  • 什么是j,为什么需要j==0
  • 这是我尝试增加令牌并转到下一个令牌以添加到 playerList
  • 为什么投反对票?你们不要乱用代码,看看什么有效/无效吗? ...

标签: java split token bufferedreader


【解决方案1】:

对于您的 addCard 方法,您需要:

void addCard(String lineToParse){

    String[] tokens = lineToParse.split("##########");
    for(int i=0; i<tokens.length; i++) {
        playerList.add(tokens[i]);
    }
}

【讨论】:

  • 我将使用这段代码。这是我尝试增加令牌以便我可以添加下一张卡片
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
相关资源
最近更新 更多