JAVA读取输入的问题

JAVA读取输入的问题,java,input,Java,Input,我正在做一个关于类和对象的家庭作业。在它里面,我有一个Address类、Letter类和PostOffice类,当调用PostOffice时,它会获取一个输入文件并扫描它,以它应该出现在信件上的方式打印输入文件中的所有内容(to:bla、from:bla、postage amount、to和from Address等) 我得到一个错误,如下所示: Exception in thread "main" java.util.NoSuchElementException: No line found

我正在做一个关于类和对象的家庭作业。在它里面,我有一个Address类、Letter类和PostOffice类,当调用PostOffice时,它会获取一个输入文件并扫描它,以它应该出现在信件上的方式打印输入文件中的所有内容(to:bla、from:bla、postage amount、to和from Address等)

我得到一个错误,如下所示:

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;
    } 
}
这是文本文件的内容

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) 斯图·施泰纳(新线) 123惰行车道(新线) 伊利诺伊州斯莱克维尔09035(新线) 汤姆·卡波尔(新线) 999电脑呆子法庭(新线) 北卡罗来纳州德韦布斯维尔28804-1359(新线) 0.50(新线) 汤姆·卡波尔(新线) 999电脑呆子法庭(新线) 北卡罗来纳州德韦布斯维尔28804-1359(新线) 克里斯·彼得斯(新线) 123某街(新线) 加利福尼亚州安尼敦92111-0389(新线) 1.55(新线) 一切都会编译,我只需要它输出如下:

---------------------------------------------------------------------------- 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) ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- From:From value邮资value 从地址值(确保并使用地址到字符串) 对:对价值 地址值(请确保并使用地址字符串) ----------------------------------------------------------------------------
如果您有下一行代码,可能会错过测试:


如果您有下一行,您可能会错过测试:

您在
循环的1次迭代中多次执行
s.readline()
。这就是为什么会出现错误

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

代码中的示例:

while(s.hasNext()){
        toName = s.nextLine();
        toStreet = s.nextLine();
        temp = s.nextLine();
        // ...
    }
这是对nextLine的3个电话,你怎么确定还有电话线

解决方案:

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

循环的1次迭代中,您多次执行
s.readline()
。这就是为什么会出现错误

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

代码中的示例:

while(s.hasNext()){
        toName = s.nextLine();
        toStreet = s.nextLine();
        temp = s.nextLine();
        // ...
    }
这是对nextLine的3个电话,你怎么确定还有电话线

解决方案:

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


问题在于不匹配(
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()
结合使用


请检查
扫描仪

问题在于不匹配(
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()
结合使用


请检查
扫描仪

您的怀疑是对的:您没有打开文件,只是将字符串“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行的倍数时不会)。
输入格式的选择不是很好,但因为它是家庭作业,我想这不是你真正的选择。为了使代码至少不那么容易出错,您可以在下一行之前询问是否有下一行(这不是一个好的解决方案,但这是一个不用做太多工作的解决方案)。

您的怀疑是正确的:您没有打开文件,只是将字符串“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行的倍数时不会)。
输入格式的选择不是很好,但因为它是家庭作业,我想这不是你真正的选择。为了使代码至少不那么容易出错,您可以在下一行之前询问是否真的有下一行(这不是一个好的解决方案,而是一个不用做太多工作就可以完成的解决方案)。

您的问题陈述并不清楚。是否生成了任何输出?或者是正在生成输出,但输出错误?根本没有输出。它在postoffice main中给出了一个错误:线程“main”java.util.NoSuchElementException中的异常:在postoffice.main(postoffice.java:14)的java.util.Scanner.nextLine(Scanner.java:1516)中找不到任何行如果您可以编辑原始问题以包含错误代码以及格式良好的输入文件示例(几行就足够了),这将非常有帮助。@anthony您能接受解决问题的答案吗?您的问题陈述中不清楚您的问题。是否生成了任何输出?或者是正在生成输出,但输出错误?根本没有输出。它在postoffice main中给了我一个错误,它说:线程“main”java.util.NoSuchElementException中的异常:在postoffice.main(postoffice.java:14)中的java.util.Scanner.nextLine(Scanner.java:1516)中找不到任何行。如果您可以编辑原始问题t,那将非常有用