简体   繁体   English

为什么会收到NoSuchElementException?

[英]Why am I getting a NoSuchElementException?

I'm sure this just comes down to me not understanding StringTokenizer, but I can't find an answer for this anywhere. 我敢肯定,这归结于我对StringTokenizer的理解,但是我在任何地方都找不到答案。 Why am I getting this error? 为什么会出现此错误?

     import java.io.*;
import java.util.StringTokenizer;
import java.util.Scanner;


public class NHLDemo {
    public static void main(String[] args) throws FileNotFoundException{
        File file = new File("nhlstats");
        Scanner inputFile = new Scanner(file);
        PlayerRecord pr;
        NHLStats list = new NHLStats();
        while(inputFile.hasNext())
        {
            String line = inputFile.next();
            StringTokenizer token = new StringTokenizer(line, "\t");
            while(token.hasMoreTokens()){
                System.out.print(token.nextToken());
                System.out.print(token.nextToken());
                String name = token.nextToken();
                String position = token.nextToken();
                String team = token.nextToken();
                int gp = Integer.parseInt(token.nextToken());
                int g = Integer.parseInt(token.nextToken());
                int a = Integer.parseInt(token.nextToken());
                int pim = Integer.parseInt(token.nextToken());
                int sog = Integer.parseInt(token.nextToken());
                int gwg = Integer.parseInt(token.nextToken());
                pr = new PlayerRecord(name, position, team, gp, g, a, pim, sog, gwg);
                list.add(pr);
            }
        }
        list.enumerate();
    }
} 

I am reading data from a specific file which has the same amount of tokens on each line 我正在从特定文件读取数据,该文件在每一行上具有相同数量的令牌

(ex. St.Louis RW TB 48 17 43 14 112 2 (例如,圣路易斯RW TB 48 17 43 14 112 2

Stamkos C TB 48 29 28 32 157 2 Stamkos C TB 48 29 28 32 157 2

Ovechkin RW WSH 48 32 24 36 220 4 Ovechkin RW WSH 48 32 24 36 220 4

Crosby C PIT 36 15 41 16 124 1 etc.) Crosby C PIT 36 15 41 16 124 1等)

when you do String line = inputFile.next(); 当您执行String line = inputFile.next();时 line is just St.Louis, so when you try nextToken you get the exception 这行只是St.Louis,所以当您尝试nextToken时会得到异常

this works: 这有效:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class MyStringTokenizer {
    public static void main(String[] args){
        try{
        File file = new File("nhlstats");
        BufferedReader input=new BufferedReader(new FileReader(file));
        //Scanner inputFile = new Scanner(file);
        PlayerRecord pr;
        NHLStats list = new NHLStats();
        String line="";
        while((line=input.readLine())!=null)
        {
            if(!line.equals("")){
            StringTokenizer token = new StringTokenizer(line, "\t");
            String name = token.nextToken();
            String position = token.nextToken();
            String team = token.nextToken();
            int gp = Integer.parseInt(token.nextToken());
            int g = Integer.parseInt(token.nextToken());
            int a = Integer.parseInt(token.nextToken());
            int pim = Integer.parseInt(token.nextToken());
            int sog = Integer.parseInt(token.nextToken());
            int gwg = Integer.parseInt(token.nextToken());
            pr = new PlayerRecord(name, position, team, gp, g, a, pim, sog, gwg);
            list.add(pr);
            }
        }
        input.close();
        list.enumerate();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

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

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