2012-02-14 146 views
0

我在我的java項目中收到下面的異常。NoSuchElementException掃描器java

Exception in thread "main" java.util.NoSuchElementException 
    at java.util.Scanner.throwFor(Unknown Source) 
    at java.util.Scanner.next(Unknown Source) 
    at com.ooad.ooadfirstassignment.Employee.readData(Employee.java:25) 
    at com.ooad.ooadfirstassignment.Salaried.readData(Salaried.java:16) 
    at com.ooad.ooadfirstassignment.Factory.<init>(Factory.java:21) 
    at com.ooad.ooadfirstassignment.MainClass.main(MainClass.java:25) 

的代碼如下:

================================= ====================

public class MainClass { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) throws IOException 
    { 
     // TODO Auto-generated method stub 
     FileInputStream empTextStreamIn = null; 
     try 
     { 
      empTextStreamIn = new FileInputStream("Employee.txt"); 
     } 
     catch(FileNotFoundException fex) 
     { 
      System.out.println("Employee File not found"); 
      fex.printStackTrace(); 
     } 
     Factory f = new Factory(empTextStreamIn); 
     empTextStreamIn.close(); 

    } 

} 

======================= =============================================

public class Factory 
{ 
    public Factory(FileInputStream empTextStreamIn) 
    { 
     // TODO Auto-generated constructor stub 
     int empType; 
     String EmpID = null,DeptID = null; 
     double salary=0; 
     Scanner sc = new Scanner(empTextStreamIn); 
     while(sc.hasNextLine()) 
     { 
      empType = sc.nextInt(); 
      switch (empType) 
      { 
       case 1:Salaried salr = new Salaried(empTextStreamIn); 
         salr.readData(); 

             System.out.println("EmpType="+empType+" EmpID="+EmpID+" DeptID="+DeptID+" Salary="+salary); 

代碼繼續,所以這是主要部分。

下一個Employee類

public class Employee 
{ 
    String EmpID, DeptID; //Unique detail for Employee class 

    protected Scanner sc; 

    Employee() 
    { 

    } 
    public Employee(FileInputStream empTextStreamIn) 
    { 
     // TODO Auto-generated constructor stub 
     sc = new Scanner(empTextStreamIn); 

    } 
    void readData() 
    { 
     String EmpID = sc.next(); 
     String DeptID = sc.next(); 
    } 

}

工薪階層

public class Salaried extends Employee 
{ 
    double salary; //Unique detail for Salaried class 

    public Salaried(FileInputStream empTextStreamIn) 
    { 
     super(empTextStreamIn); 

    } 
    void readData() 
    { 
     super.readData(); 
     salary = sc.nextDouble(); 


    } 

} 

請幫助我,我要去哪裏錯了。

+0

downvote的任何特定原因? – 2012-02-14 07:57:47

+0

即使下面這行是空的,hasNextLine()是否返回true?你有沒有試圖用換行符結束文件? (即最後一行末尾沒有換行符) – 2012-02-14 08:18:59

+0

將Employee類中的readDate函數更改爲「while(sc。hasNextLine()) { \t empType = sc.nextInt(); \t EmpID = sc.next(); \t DeptID = sc.next(); }「和工資readData()函數作爲」super.readData(); while(sc.hasNext()) salary = sc.nextDouble(); – 2012-02-14 13:15:35

回答

1

這裏的問題是Scanner類使用內部緩衝區。您在Factory類中創建了一個Scanner對象。這Scanner從底層FileInputStream讀取到它自己的緩衝區中。我懷疑你的輸入文件很小,整個文件都適合這個緩衝區。這意味着Factory中的Scanner將通讀整個FileInputStream。在此之後,您將在Employee類中使用相同的FileInputStream創建一個新的掃描儀對象。但是,第一個Scanner已經佔用了該FileInputStream中的所有內容。因此,由於沒有數據,Scanner會引發異常。

因此,您需要的僅僅是確保在從文件讀取時使用相同的Scanner對象,而不是在同一個FileInputStream上實例化幾個不同的掃描儀。

<編輯>

爲了驗證這種情況下,你可以嘗試插入while循環內以下行Factory類:

System.out.println("Data could be read from the InputFileStream: " 
        + (empTextStreamIn.read() != -1)); 

(請注意,讀取方法可以拋出IOException,所以你將不得不圍繞它與一個try-catch塊)

< /編輯>

+0

@ Alderath正如你猜測的一樣,可以從InputFileStream中讀取數據:false多次 – 2012-02-14 16:38:08

2

似乎您的測試文件包含的數據比您嘗試閱讀的數據要少,因爲這就是爲什麼this exception is thrown。最好使用hasNext()方法檢查輸入是否有任何下一個元素。

+0

我在Factory類中使用了hasNextLine()。但仍然存在錯誤。 – 2012-02-14 08:03:53

+0

每次在下一個 – MByD 2012-02-14 08:15:20

+0

使用它時使用它,但它顯示的值爲空 – 2012-02-14 13:14:09