Java的。阅读输入时出现问题

时间:2012-01-17 20:08:16

标签: java input

我正在做一个处理课程和对象的家庭作业。在其中,我有一个Address类,Letter类和PostOffice类,在调用PostOffice时,获取输入文件并对其进行扫描,基本上打印输入文件中的所有内容,就像它应该出现在字母上一样(to:bla,from :bla,邮资金额,往返地址等)

我收到的错误是:

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)

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

4 个答案:

答案 0 :(得分:4)

您在s.readline()循环的1次迭代中多次执行while次。这就是你收到错误的原因。

您只需在while循环中调用readLine()

代码中的示例:

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

这是对nextLine的3次调用,您如何确定仍有行?

<强> SOLUTION:

在除第一个之外的每个s.nextLine()之前放置一个if语句。 例如:

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

答案 1 :(得分:2)

答案 2 :(得分:0)

问题在于错误匹配(hasNext()nextLine())。

使用nextLine(),您应该使用hasNextLine()。这可能是您需要的组合,因为您似乎逐行阅读。

e.g。查看你的文本文件应该如下所示,但是如果文本文件并不总是很好的话,必须进行修改:

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()结合使用。

请完成Scanner doc

答案 3 :(得分: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之前,你可以提出至少一点点容易出错的问题。