首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java.util.scanner错误

Java.util.scanner错误
EN

Stack Overflow用户
提问于 2013-10-01 08:05:02
回答 1查看 5.4K关注 0票数 0

我已经构建了这个程序,它将从一个文件中读取记录,并基于一个Student类构建一个学生数组。

**StudentTest类

代码语言:javascript
复制
import java.io.*;
import java.util.Scanner;

public class StudentTest 
{
    public static void main(String[] args) 
    {
    String name;
    String address;
    String major;
    double gpa;
    int classLevel;
    int college;
    String blank;
    String idNumber;

    Scanner fileIn = null;
    try
    {
        fileIn = new Scanner (new FileInputStream("student.dat"));
    }
    catch (FileNotFoundException e)
    {
        System.out.println("File not found");
        System.exit(0);
    }

    Student[] aStudent = new Student[15];
    int index = 0;
    if (fileIn.hasNext())
    {
    for (index=0; index <= 15; index++)
    {
        blank = fileIn.nextLine();
        name = fileIn.nextLine();
        //System.out.println("Name: " + name);
        address = fileIn.nextLine();
        //System.out.println("Address: " + address);
        major = fileIn.nextLine();
        gpa = fileIn.nextDouble();
        //System.out.println("Major: " + major + " GPA: " + gpa);
        classLevel = fileIn.nextInt();
        college = fileIn.nextInt();
        //System.out.println("Class Level: " + classLevel + " college " + college);
        idNumber = fileIn.nextLine();
        aStudent[index] = new Student(name, address, major, gpa, classLevel, college, idNumber);
        //System.out.println("===================================");
        aStudent[index].Display();
    }
    }
    else
    {
    fileIn.close();
    }
   }
     }

**学生班

代码语言:javascript
复制
    import java.util.Scanner;

    public class Student 
       {
       private String name;
       private String address;
       private String major;
       private double gpa;
       private int classLevel;
       private int college;
       private String idNumber;
       Scanner keyboard = new Scanner(System.in);

    public Student(String name, String address, String major, double gpa, int classLevel, int coll, String idNum)
    {
        this.name = name;
        this.address = address;
        this.gpa = gpa;
        this.major = major;
        this.classLevel = classLevel;
        this.college = coll;
        this.idNumber = idNum;

    }
    public String getName()
    {
        return name;
    }
    public String getAddress()
    {
        return address;
    }
    public String getMajor()
    {
        return major;
    }
    public double getGPA()
    {
        return gpa;
    }
    public int getClassLevel()
    {
        return classLevel;
    }
    public int getCollege()
    {
        return college;
    }
    public String getID()
    {
        return idNumber;
    }
    public void setAddress(String address)
    {
    }
    public void setMajor(String maj)
    {
    }
    public void setCollege(int coll)
    {
    }
    public void Display()
    {
        System.out.println("Name: "+getName());
        System.out.println("Address: "+getAddress());
        System.out.println("Major: " + getMajor());
        System.out.println("GPA: "+getGPA()+" Class Level: "+getClassLevel()+" College: "+getCollege());
        System.out.println("ID: "+getID());
        System.out.println("===============================");
    }
}

这是文件(student.dat)

代码语言:javascript
复制
Doe John D 
123 Fake St
IS 
4.0 
4 
15
M123456789

Smith Thomas F 
345 Lakeside Ln 
ACCT 
3.0
2 
14
M235896135

当我运行它时,这是我的输出

代码语言:javascript
复制
Name: Doe John
Address: 123 Fake St
Major: IS 
GPA: 4.0 Class Level: 4 College: 15
ID: 
===============================
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.nextDouble(Unknown Source)
at StudentTest.main(StudentTest.java:41)

谢谢

EN

回答 1

Stack Overflow用户

发布于 2013-10-01 08:08:33

因此,如果您多次调用nextInt(),然后调用nextLine(),那么nextLine()调用将从前面的nextInt()调用中获取EOL令牌。一种解决方案是,用nextLine()跟踪您的nextInt()调用,只是为了吞下EOL令牌。

例如,更改以下内容:

代码语言:javascript
复制
    college = fileIn.nextInt();
    idNumber = fileIn.nextLine();

要这样做:

代码语言:javascript
复制
    college = fileIn.nextInt();
    fileIn.nextLine();  // handle the EOL token
    idNumber = fileIn.nextLine();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19105865

复制
相关文章

相似问题

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