繁体   English   中英

为什么这段代码不读取我的文件?

[英]Why doesn't this code read my file?

这是我从文本文件读取的代码:

    public RecordManager(){
    int pos;
    String athlete, record, country, raceTime;
    try {
        Scanner scFile = new Scanner(new File("resultdata.txt"));
        while(scFile.hasNext()){
            Scanner sc = new Scanner(scFile.next()).useDelimiter("#");
            athlete = sc.next();
            country = sc.next();
            pos = sc.nextInt();
            record = sc.next();
            raceTime = sc.next();                
            sc.close();
            if("WRC".equals(record)){
                resultArr[size] = new WorldRecord(athlete, country, pos, raceTime);
            }
            else if("OLR".equals(record)){
                resultArr[size] = new OlympicRecord(athlete, country, pos, raceTime);
            }
            else{
                resultArr[size] = new RaceResult(athlete, country, pos, raceTime);
            }
            size++;
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
    }

这是文本文件中的内容:

Carey Blem#ITA#6#---#4m49.8 
Tammera Hoesly#POR#1#---#4m6.2 
Toi Swauger#FRA#1#OLR#51.3 
Moises Mellenthin#ZIM#2#---#4m34 
Madelene Mcclennon#LUX#1#WRC#1m52.7 
Lashon Meisenheimer#RSA#1#---#2m31.2

我一直在努力,但是我一直在得到这个:

run:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at it.practical.training.RecordManager.<init>(RecordManager.java:29)
at it.practical.training.SimpleInterface.main(SimpleInterface.java:20)
Java Result: 1
BUILD SUCCESSFUL (total time: 13 seconds)

请告诉我怎么了。

当您使用next()它将读取下一个单词。 您的第一行包含两个单词CareyBlem#ITA#6#---#4m49.8 当您从一行中读取单词时,您需要使用nextLine()转到下一行。

我怀疑你真正想要的是

Scanner sc = new Scanner(scFile.nextLine()).useDelimiter("#");

一次读取整行。

我不完全确定出了什么问题,但是我根本不会使用Scanner来读取文件。 我将使用BufferedReader ,并在readline方法不返回null的情况下循环。 然后,我将使用String类的split方法来获取所需的信息。 例如

public RecordManager(){
  int pos;
  String line;
  String[] info;
  try {
    BufferedReader scFile = new BufferedReader(new FileReader(new File("resultdata.txt")));
    line = scFile.readLine();
    while(line != null){
        info = line.split("#");//now athlete is in index 0, country at index 1 etc              
        if("WRC".equals(info[3])){
            resultArr[size] = new WorldRecord(info[0], info[1], info[2], info[4]);
        }
        else if("OLR".equals(info[3])){
            resultArr[size] = new OlympicRecord(info[0], info[1], info[2], info[4]);
        }
        else{
            resultArr[size] = new RaceResult(info[0], info[1], info[2], info[4]);
        }
        size++;
        line = scFile.readLine();
     }
  } catch (FileNotFoundException ex) {
    Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
  }
}

我知道它并不能直接回答您的代码,但是它提供了一种替代方法来满足您的需求。 我希望它会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM