2013-10-01 4 views
0

필자는 파일에서 레코드를 읽고 Student 클래스를 기반으로 학생 배열을 빌드하는이 프로그램을 작성했습니다.Java.util.scanner errors

** StudentTest 클래스

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(); 
    } 
    } 
    } 

** 여기

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("==============================="); 
    } 
} 

및 학생 클래스는 내가 그것을 실행하면

Doe John D 
123 Fake St 
IS 
4.0 
4 
15 
M123456789 

Smith Thomas F 
345 Lakeside Ln 
ACCT 
3.0 
2 
14 
M235896135 

는, 이것이 파일 (student.dat)입니다 내 출력

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) 

감사

+3

좋아. 우선, 너무 많은 코드를 제공했습니다. 둘째,이 줄은 :'StudentTest.main (StudentTest.java:41)'은 오류를받는 정확한 클래스 ('StudentTest.java')와 줄 번호 ('41')를 가리 킵니다. 이 라인은 무엇입니까? 힌트 : 나는 41 줄을 셀 수 없다. – nhgrif

답변

1

한 스캐너 방법은 행의 끝 (EOL) 토큰을 처리 할 것을 이해하고, 그 nextLine(), 그리고 다른 모든하지 않습니다. 따라서 nextInt() 번을 호출 한 다음 nextLine()으로 전화하면 nextLine() 호출은 이전 nextInt() 호출에서 EOL 토큰을 잡아 당깁니다. 한 가지 해결책은 nextLine()으로 nextInt() 전화를 받으면 EOL 토큰을 삼키는 것입니다.

예를 들어, 변경이 행

college = fileIn.nextInt(); 
    idNumber = fileIn.nextLine(); 

을 :

college = fileIn.nextInt(); 
    fileIn.nextLine(); // handle the EOL token 
    idNumber = fileIn.nextLine(); 
+0

감사합니다. 그 문제가 해결되었습니다. 이 프로그램은 현재 두 학생을 출력하고 있지만 여전히 "java.util.Scanner.hasNext (알 수없는 소스)"오류가 발생합니다. 내 루핑 중에 오류가 있다고 가정합니다. 어떻게 해결해야합니까? – user1873736

+1

@ user1873736 : 나, 나는 모든'nextXXX()'를 앞의'hasNextXXX()'와 연결하기 때문에 오류가 거의 없다. –