簡體   English   中英

讀取文件時輸入不匹配異常

[英]Input Mismatch Exception while reading file

到目前為止,這是我的代碼。 我試圖將文本文件讀入演示程序,並將每一行分別讀入另一個類。 無論出於何種原因,該代碼都會在第一行和第二行之間添加一個空行,這會導致我的程序失敗,因為nextInt()命令由於空行而讀取了String。 任何幫助將不勝感激。 多謝你們。

文本文件:

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)

在使用.nextInt()讀取int之后,游標就在int之后,而不是在下一行。 因此,此時調用.nextLine()只會給您一個空字符串。

使用nextInt()讀取整數后,調用ath.nextLine(); 在讀取下一個人的名字之前,將光標放在正確的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM