从CSV文本文件中扫描变量

时间:2016-06-06 06:13:45

标签: java

嗨,我正在努力扫描并从CSV文本文件中分配变量,下面的错误是我在运行时得到的,并且不知道出了什么问题

Exception in thread "main" java.util.NoSuchElementException</p>
at java.util.Scanner.throwFor(Unknown Source)</p>
at java.util.Scanner.next(Unknown Source)</p>

public Moves(String role) throws FileNotFoundException{

    //Concatenate filename
    File file = new File("/moves/" + role + "Moves");
    //Declare and initialise scanner
    Scanner scanner = new Scanner("/moves/" + role + "Moves");
    //Loops through move file, assign variables from file
    while (scanner.hasNextLine()) {
        name       = scanner.next().trim();
        damageType = scanner.next().trim();
        minDamage  = scanner.nextInt();
        maxDamage  = scanner.nextInt();
        minHeal    = scanner.nextInt();
        maxHeal    = scanner.nextInt();
        recurringDamage = scanner.nextInt();
        damageCounter   = scanner.nextInt();
        aoeSize         = scanner.nextInt();
        description     = scanner.next().trim();
        System.out.println(name);
        System.out.println(damageType);
        System.out.println(minDamage);
        System.out.println(maxDamage);
        System.out.println(minHeal);
        System.out.println(maxHeal);
        System.out.println(recurringDamage);
        System.out.println(damageCounter);
        System.out.println(aoeSize);
        System.out.println(description);
    }
    scanner.close();    
}

1 个答案:

答案 0 :(得分:0)

将'file'参数传递给scanner。

    File file = new File("/moves/" + role + "Moves");
    //Declare and initialise scanner
    Scanner scanner = new Scanner(file );