2012-01-17 81 views
0

我正在做一個處理類和對象的作業。在它中,我有一個Address類,Letter類和PostOffice類,它們在調用PostOffice時會接收一個輸入文件並掃描它,基本上以輸入文件中所有內容的方式將其顯示在一個字母上(如:bla,from :BLA,郵資金額,並從地址等)Java。閱讀輸入時遇到的問題

我得到一條錯誤:

Exception in thread "main" java.util.NoSuchElementException: No line found 
at java.util.Scanner.nextLine(Scanner.java:1516) 
at PostOffice.readLetters(PostOffice.java:33) 
at PostOffice.main(PostOffice.java:14) 

和我真的不明白爲什麼....

這裏是我的崗位辦公室班級:

import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

public class PostOffice { 

private final int MAX = 1000; 
private Letter [] letterArray = new Letter[MAX]; 
private int count; 

public static void main(String [] args) { 
    PostOffice postOffice = new PostOffice(); 
    postOffice.readLetters("letters.in"); 
    postOffice.sortLetters(); 
    postOffice.printLetters(); 
} 

public PostOffice() { 
    Letter [] myLetters = letterArray; 
    this.count = 0; 
} 

public void readLetters(String filename) { 
    String toName, toStreet, toCity, toState, toZip; 
    String fromName, fromStreet, fromCity, fromState, fromZip, temp; //, weight; 
    double weight; 
    int index; 
    Scanner s = new Scanner(filename); 
    if (s != null) { 
     while(s.hasNext()){ 
      toName = s.nextLine(); 
      toStreet = s.nextLine(); 
      temp = s.nextLine(); 
      index = temp.indexOf(","); 
      toCity = temp.substring (0, index); 
      index = index + 2; 
      toState = temp.substring (index, index + 2); 
      toZip = temp.substring (index); 
      fromName = s.nextLine(); 
      fromStreet = s.nextLine(); 
      temp = s.nextLine(); 
      index = temp.indexOf(","); 
      fromCity = temp.substring (0, index); 
      index = index + 2; 
      fromState = temp.substring (index, index + 2); 
      fromZip = temp.substring (index); 
      String var = s.nextLine(); 
      weight = Double.parseDouble(var); 
      //weight = s.nextLine(); 
      Letter l = new Letter(toName, toStreet, toCity, toState, toZip, fromName, fromStreet, fromCity, fromState, fromZip, weight); 
      this.count += 1; 
      this.letterArray[count - 1] = l; 
     } 
    } 
    s.close(); 
} 

public static void sortLetters() { 
//call sortSearchUtil This method should call the compareTo method provided by the Letter class to sort. 
//You may use any sorting routine you wish (see SortSearchUtil.java) 
} 

public static void printLetters() { 
//call tostring of letter class. print the count of Letters followed by the total postage followed 
//by each Letter (make sure you use the toString method provided by the Address and Letter classes for this) 
} 

}

我的信類:

public class Letter extends PostOffice implements Comparable<Letter> { 
private static final double POSTAGE_RATE = 0.46; 
private String fromName; 
private Address fromAddr; 
private String toName; 
private Address toAddr; 
private double weight; 


    public Letter (String fromName, String fromStreet, String fromCity, String fromState,  String fromZip, String toName, 
String toStreet, String toCity, String toState, String toZip, double weight) { 
this.fromName = fromName; 
this.fromAddr = new Address(fromStreet, fromCity, fromState, fromZip); 
this.toName = toName; 
this.toAddr = new Address(toStreet, toCity, toState, toZip); 
this.weight = weight; 
} 

public String toString() { 
String result; 
result = String.format("from: %s\t\t\t%5.2f\n%s", fromName, getPostage(weight), fromAddr); 
result = result + String.format("\t\t To: %s\n\t\t%s", toName, toAddr); 
return result; 
} 

    public int compareTo(Letter that) { 
int value; 
value = this.toAddr.getZip().compareTo(that.toAddr.getZip()); 
return value; 
} 


public static double getPostage(double weight) { 
double workWeight; 
workWeight = weight + 0.999; 
workWeight = (int)workWeight; 
return workWeight * POSTAGE_RATE; 
    } 
} 

和我的地址類:

import java.awt.*; 
import java.util.*; 

public class Address { 
private String street; 
private String city; 
private String state; 
private String zip; 

    public Address (String street, String city, String state, String zip) { 
    this.street = street; 
    this.city = city; 
    this.state = state; 
    this.zip = zip; 
    } 

    public String getStreet() { 
    return street; 
    } 

    public void setStreet(String street) { 
    this.street = street; 
    } 

    public String getCity() { 
    return city; 
    } 

    public void setCity(String city) { 
    this.city = city; 
    } 

    public String getState() { 
    return state; 
    } 

    public void setState(String state) { 
    this.state = state; 
    } 

    public String getZip() { 
    return zip; 
    } 

    public void setZip(String zip) { 
    this.zip = zip; 
    } 

    public String toString() { 
    String result; 
    result = String.format("%s\n%s, %s %s", street, city, state, zip); 
    return result; 
    } 
} 

,這是文本文件

 
Stu Steiner (NEW LINE) 
123 Slacker Lane (NEW LINE) 
Slackerville, IL 09035 (NEW LINE) 
Tom Capaul(NEW LINE) 
999 Computer Nerd Court (NEW LINE) 
Dweebsville, NC 28804-1359 (NEW LINE) 
0.50 (NEW LINE) 
Tom Capaul (NEW LINE) 
999 Computer Nerd Court (NEW LINE) 
Dweebsville, NC 28804-1359 (NEW LINE) 
Chris Peters (NEW LINE) 
123 Some St. (NEW LINE) 
Anytown, CA 92111-0389 (NEW LINE) 
1.55 (NEW LINE) 

一切編譯的內容,我只需要它像這樣輸出:

 
---------------------------------------------------------------------------- 

From: From value            Postage value 
From Address value (be sure and use Address toString) 

        To: To value 
        To Address value (be sure and use Address toString) 

---------------------------------------------------------------------------- 
+1

您的問題不清楚從您的問題陳述。是否有任何產出?或者是有輸出產生,但它是錯誤的輸出? – 2012-01-17 20:11:24

+0

根本沒有輸出。它給我一個錯誤郵局主要說:線程「主」異常java.util.NoSuchElementException:沒有找到行 \t at java.util.Scanner.nextLine(Scanner.java:1516) \t at PostOffice.readLetters(PostOffice .java:33) \t at PostOffice.main(PostOffice.java:14) – anthony 2012-01-17 20:12:30

+0

如果你可以編輯你的原始問題來包含錯誤代碼以及一個合適的輸入文件例子(幾行將足夠)。 – tim 2012-01-17 20:34:38

回答

4

你做s.readline()多次內1次迭代while循環。這就是你遇到錯誤的原因。

你需要調用readLine()只有一次while循環

例中在你的代碼:

while(s.hasNext()){ 
     toName = s.nextLine(); 
     toStreet = s.nextLine(); 
     temp = s.nextLine(); 
     // ... 
    } 

這些都是3調用nextLine,怎麼你肯定還是有行?

SOLUTION:

放的,如果每個s.nextLine前聲明()除了第一個。 實施例:

while(s.hasNext()){ 
    toName = s.nextLine(); 
    if (s.hasNext()) { 
     toStreet = s.nextLine(); 
    } 
    if (s.hasNext()) { 
     temp = s.nextLine(); 
    } 
    // ... 
} 
+0

是的,我知道它的壞,我對此有點新。但被賦予一個文本文件,內容如下:斯圖·施泰納 123懶鬼巷 Slackerville,IL 09035 湯姆Capaul 999玩電腦的呆子法院 Dweebsville,NC 28804-1359 0.50 湯姆Capaul 999玩電腦的呆子法院 Dweebsville, NC 28804-1359 Chris Peters 123 Some St. Anytown,CA 92111-0389 1.55所以我期待有下一行。對不起,我不知道如何正確評論這個,但有14行代碼 – anthony 2012-01-17 20:22:56

+3

Adel Boutros,在你的回答中的評論「你的程序太糟糕了」沒有幫助或適當的。 – 2012-01-17 20:28:50

+0

球員我知道.nextLine會導致問題,因爲沒有下一行,但那是因爲掃描器正在讀取letters.in這個單詞,而不是掃描該文件並檢查該文件是否有下一行。那是什麼林有麻煩。我調試並將其設置爲toStreet = s.nextLine();它只是失敗。如果你們可以找出替代方案或幫助我提供有關如何正確掃描此文本文件的建議,我將非常感激:) – anthony 2012-01-17 20:36:50

0

的問題是mistmatch(hasNext()nextLine())。您可以使用hasNextLine()。這可能是您需要的組合,因爲您似乎逐行讀取它。

例如看你的文本文件的東西如下而是應該修改時,必須注意進行,如果文本文件並不總是很好地形成:

while(s.hasNextLine()) { // check if there is next line 
    String toName = s.nextLine(); 
    String toAddrLine1 = s.nextLine(); 
    String toAddrLine2 = s.nextLine(); 
    String fromName = s.nextLine(); 
    String fromAddrLine1 = s.nextLine(); 
    String fromAddrLine2 = s.nextLine(); 
    String postageValueStr = s.nextLine(); 

    //do further processing down here 

雖然hasNext()應結合使用next()

請通過Scannerdoc

0

您的懷疑是對的:您沒有打開文件,只是將字符串「letter.in」傳遞給掃描儀。正確的代碼是:

Scanner s = null; 
    try { 
     s = new Scanner(new File(filename)); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(PostOffice.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    if (s != null) { 
     .... 
    } 

你的程序仍然不會打印出任何東西作爲printLetters()方法未實現,但它不會了(至少不是如果文件總是有7行的倍數崩潰)。 輸入格式不是很好選擇,但因爲它是作業,我想這不是你的選擇。如果實際上有下一行(這不是一個好的解決方案,而是一個沒有太多工作的方法),那麼在每個nextline之前,可以讓代碼至少減少一點錯誤。