读取文件时输入不匹配异常

时间:2014-11-22 23:22:39

标签: java eclipse exception input filereader

到目前为止,这是我的代码。我正在尝试将文本文件读入我的演示程序,并将每行分别读入另一个类。无论出于何种原因,代码在第一个等级和下一个名称行之间添加一个空行,这个行在一个术语中导致我的程序失败,因为nextInt()命令因空行而读取一个String。任何帮助将不胜感激。谢谢你们。

TEXT FILE:

David Wise
USA
SKI HALFPIPE MENS
1
Mike Riddle
Canada
SKI HALFPIPE MENS
2
Kevin Rolland
France
SKI HALFPIPE MENS
3
Maddie Bowman
USA
SKI HALPIPE WOMENS 
1
Marie Martinod
France
SKI HALPIPE WOMENS 
2
Ayana Onozuka
Japan
SKI HALPIPE WOMENS 
3
Felix Loch
Germany
LUGE SINGLE MENS
1
Albert Demchenko
Russia
LUGE SINGLE MENS
2
Armin Zoeggeler
Italy
LUGE SINGLE MENS
3
Natalie Geisenberger
Germany
LUGE SINGLE WOMENS
1
Tatjana Huefner
Germany
LUGE SINGLE WOMENS
2
Erin Hamlin
USA
LUGE SINGLE WOMENS
3
Alexander Tretiakov
Russia
SKELETON MENS
1
Martins Dukurs
Latvia
SKELETON MENS
2
Mathew Antoine
USA
SKELETON MENS
3
Elizabeth Yarnold
United Kingdom
SKELETON WOMENS
1
Noelle Pikus-Pace
USA
SKELETON WOMENS
2
Elena Nikitina
Russia
SKELETON WOMEN
3
Michel Mulder
Russia
500M SPEED SKATING MENS
1
Jan Smeekens
Russia
500M SPEED SKATING MENS
2
Ronald Mulder
Russia
500M SPEED SKATING MENS
3

演示计划:

import java.io.*;
import java.util.*;
import ch06.lists.*;
import support.*;

public class OlympicDemo 
{
    public static void main(String[] args)
    {
    ListInterface<String> list = new ArrayIndexedList<String>();
    final String FILEINNAME = "src/AthleteInformation.txt";

    String name = "";
    String country = "";
    String event = "";
    int rank = 0;

    Athlete athletes;
    OlympicAthletes olympic;

    try
    {
        FileReader file = new FileReader(FILEINNAME);
        Scanner ath = new Scanner(file);

        while(ath.hasNext())
        {
            name = ath.nextLine();
            System.out.println(name);
            country = ath.nextLine();
            System.out.println(country);
            event = ath.nextLine();
            System.out.println(event);
            rank = ath.nextInt();
            System.out.println(rank);

            athletes = new Athlete(name, country, event, rank);
        }
    }
    catch(FileNotFoundException e)
    {
        System.out.println("FILE: " + FILEINNAME + " DOES NOT EXIST");
    }

    /**
    try
    {
        ObjectInputStream in = new ObjectInputStream(new
                FileInputStream(FILEINNAME));

        athletes = (Athlete)in.readObject();
    }
    catch(Exception e)
    {
        System.out.println("FILE: " + FILEINNAME + " DOES NOT EXIST");
    }
    */
    }
}

输出:

David Wise
USA
SKI HALFPIPE MENS
1

Mike Riddle
Canada
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at OlympicDemo.main(OlympicDemo.java:34)

1 个答案:

答案 0 :(得分:0)

.nextInt()读取int后,光标位于int之后,尚未出现在下一个新行上。因此,在此时调用.nextLine()只会为您提供一个空字符串。

使用nextInt()读取整数后,请先调用ath.nextLine();将光标放在正确的位置,然后再读取下一个人的姓名。