这台扫描仪出了什么问题?

时间:2011-12-11 09:04:52

标签: java methods java.util.scanner

我正在尝试让扫描仪扫描一个txt文件,当我总共扫描6个项目时(项目在“=”之前和之后),它可以工作,当我扫描更多时,它会中断。扫描后,我希望将值返回到主类。

public class mainClass {

public static void main(String[]args) throws FileNotFoundException {
            load loadery = new load("resources/monsters/human/humanSerf.txt");
            load.loader();
            String returned= loadery.retrn();
            System.out.println(returned);

     }
}

扫描仪类:

public class load {
static String name, value,name1,value1,name2, value2,name3,value3,name4,value4,name5,value5;
  public static void loader(String... aArgs) throws FileNotFoundException {
    load parser = new load("resources/monsters/human/humanSerf.txt");
    parser.processLineByLine();
  }

  public load(String aFileName){
    fFile = new File(aFileName);  
  }


  public final void processLineByLine() throws FileNotFoundException {

    Scanner scanner = new Scanner(new FileReader(fFile));
    try {

        scanner.useDelimiter("=");

           name = scanner.next();
           value = scanner.next();
           name1 = scanner.next();
           value1 = scanner.next();
           name2 = scanner.next();
           value2 = scanner.next();
           name3 = scanner.next();
           value3 = scanner.next();
           name4 = scanner.next();
           value4 = scanner.next();
           name5 = scanner.next();
           value5 = scanner.next();

      ;

    }
    finally {

      scanner.close();
    }


     }


      // PRIVATE 
      private final File fFile;


      public static String retrn(){
      return name+"="+ value+"   "+name1+"="+ value1+"   "+name2+"="+ value2+"   "+name3+"="+ value3+"   "+name4+"="+ value4+"   "+name5+"="+ value5;
      }
      }

txt文件:

symbol=HS
strength=15
agility=13
constitution=7
wisdom=9
intelligence=5

我该怎么做才能解决这个问题?

这些是错误

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Scanner.java:838)

at java.util.Scanner.next(Scanner.java:1347)

at load.processLineByLine(load.java:32)

at load.loader(load.java:8)

at mainClass.main(mainClass.java:9)

1 个答案:

答案 0 :(得分:0)

尝试使用

scanner.useDelimiter( Pattern.compile("[\\r\\n= ]+") );