首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我会得到一个NoSuchElementException?

为什么我会得到一个NoSuchElementException?
EN

Stack Overflow用户
提问于 2016-10-23 04:24:54
回答 1查看 140关注 0票数 1

我确信这只是因为我不理解StringTokenizer,但是我在任何地方都找不到答案。为什么我会得到这个错误?

代码语言:javascript
复制
     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();
    }
} 

我正在从一个特定的文件读取数据,该文件的每一行都有相同数量的标记

(例如St.Louis RW TB 48 17 43 14 112 2

Stamkos C TB 48 29 28 32 157 2

Ovechkin RW WSH 48 32 24 36 220 4

Crosby C坑36 15 41 16 124 1等)

EN

回答 1

Stack Overflow用户

发布于 2016-10-23 04:51:43

当您执行String line =inputFile.next()时,line就是St.Louis,所以当您尝试nextToken时,您会得到异常

这是可行的:

代码语言:javascript
复制
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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40196701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档