java - 如何使用Scanner的分隔符方法

标签 java file-io hashmap java.util.scanner delimiter

我目前正在致力于游戏《Risk》的实现。在构建国家、大陆及其邻接关系的 Board 类中,我正在阅读三个不同的文本文件。当我 build 一个新大陆时,它的 builder 期望以下内容 (字符串名称,int BonusArmies,ArrayList memberCountries)。 现在,我正在使用扫描仪读取组织如下的文本文件, 其名称、所拥有的奖励军队,每一行的其余部分是其成员国。

北美洲,5,阿拉斯加州,艾伯塔省,中美洲,美国东部,格陵兰岛,西北地区,安大略省,魁北克省,美国西部 南美洲,2,阿根廷,巴西,委内瑞拉 欧洲,5,英国,冰岛,北欧,斯堪的纳维亚半岛,南欧,乌克兰,西欧 非洲,3,刚果,东非,埃及,马达加斯加,北非,南非 亚洲,7,阿富汗,中国,印度,伊尔库茨克,日本,堪察加半岛,中东,蒙古,暹罗,西伯利亚,乌拉尔,雅库茨克 澳大利亚,2,东澳大利亚,印度尼西亚,LotR,新几内亚,西澳大利亚

我知道我需要为扫描仪设置一个分隔符,我尝试了多种方法,但没有成功。这是我尝试创建大陆 HashMap 的示例。

public void createContinents()
{   

    Scanner inputTwo = new Scanner( new File( "continents.txt");

while( inputTwo.hasNext())  //continues to create and add countries until their are none next
{   
    String line = inputTwo.nextLine();  //stores the entire line
    Scanner lineScan = new Scanner( line);  //passes the entire line into Scanner
    lineScan.useDelimiter(","); //sets the Scanner's delimiter pattern

    String name = inputTwo.next();  //stores the first String which is the name of the current continent being created
    System.out.println( " the name is" + name);

    int bonusArmies = inputTwo.nextInt();   //stores the second String which is casted into an int for the continent bonus armies
    System.out.println(" the number of bonus armies is" + bonusArmies);

    ArrayList<Country> memberCountries = new ArrayList<Country>();  //creates an arraylist to hold the member countries of current continent

    while( inputTwo.hasNext())  //goes through the rest of the line to add the member countries until it reaches the end of the line
    {   

        memberCountries.add( countries.get(inputTwo.next()));//gets string name of country and pass it as key to store into temp arraylist of countries         
        System.out.println( "the member countries" + memberCountries);
    }

        continent = new Continent( name,  bonusArmies,  memberCountries);   //creates continent
        continents.put(name , continent);   //associates a key to the current continent

}


    inputTwo.close();

 }    

Here is a picture of the error I get when test running it

这是该图像中的文本。

    PS C:\Users\repti_000\desktop\risk\homeworks2120\game> java BoardTester
     the name is
    SouthAmerica,2,Argentina,Brazil,Venezuela 

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Board.createContinents(Board.java:66)
    at Board.loadBoard(Board.java:141)
    at Board.<init>(Board.java:27)
    at BoardTester.main(BoardTester.java:7)
   PS C:\Users\repti_000\desktop\risk\homeworks2120\game>

最佳答案

我重命名了扫描仪和一些变量以使其更清晰,并添加了一些带有 CHANGED 的注释。我想知道为什么你的 HashMap 大陆看起来像大陆(名称,大陆),而你存储大陆的名称(正如你用构造函数给出的那样)。使用 Continental.getName() 似乎是更好的做法。

正如 RotN 爵士所指出的,您使用了错误的扫描仪,而且您的名字确实不清楚。尽量让你的变量名称更清晰一些,这应该有助于时间。

    public void createContinents()  { 

    //  Scan the continents.txt file
    Scanner worldScan = new Scanner( new File( "continents.txt");

    //  While worldScan has a next line, run loop
    //  CHANGED: not hasNext(), but hasNextLine()
    while( worldScan.hasNextLine()) {

        //  Create a scanner for the selected line in worldScan
        String continentLine = inputTwo.nextLine();
        Scanner continentScan = new Scanner(continentLine);
        continentScan.useDelimiter(",");

        //  Grab the continent from the scanner
        String name = continentScan.next();
        System.out.println( " the continent name is" + name);

        //  Grab the armies from the scanner
        //  CHANGED: you read it out as a string, not as an int
        int bonusArmies = Integer.toString(continentScan.next());
        System.out.println(" the number of bonus armies is" + bonusArmies);

        //  Instantiate loop variable countries
        ArrayList<Country> memberCountries = new ArrayList<Country>();

        while( continentScan.hasNext()) {
            memberCountries.add(countries.get(continentScan.next()));
            System.out.println( "the member countries" + memberCountries);
        }

        //  Instantiate loop variable continent
        Continent continent = new Continent( name,  bonusArmies,  memberCountries);

        //  Add continent to the hashmap
        continents.put(name,continent);

        continentScan.close();
    }

    worldScan.close();
 }

关于java - 如何使用Scanner的分隔符方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23033893/

相关文章:

c++ - 从资源加载原始文件

java - 如何根据日期对HashMap进行排序?

java - JPA 支持数据库 - 由 JPA 定义还是由提供商决定?

java - 使用接口(interface)实例化和具体类实例化之间的区别? (HashMap、Map & List、ArrayList)

java - 如何将文本文件存储到数组中

java - 使用 Java 8 收集数组/列表中的文件名

java - 对于 Java HashMap 上的每个循环

java - 使用 nCipherKM 解密 HSM key 时出错

javascript - 为什么 Node.js 的 fs.readFile() 返回的是缓冲区而不是字符串?

c# - FileStream 每隔一个字符返回空字符