Java。阅读输入时遇到的问题
我正在做一个处理类和对象的作业。在它中,我有一个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) ----------------------------------------------------------------------------
你做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();
}
// ...
}
是的,我知道它的坏,我对此有点新。但被赋予一个文本文件,内容如下:斯图·施泰纳 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
Adel Boutros,在你的回答中的评论“你的程序太糟糕了”没有帮助或适当的。 – 2012-01-17 20:28:50
球员我知道.nextLine会导致问题,因为没有下一行,但那是因为扫描器正在读取letters.in这个单词,而不是扫描该文件并检查该文件是否有下一行。那是什么林有麻烦。我调试并将其设置为toStreet = s.nextLine();它只是失败。如果你们可以找出替代方案或帮助我提供有关如何正确扫描此文本文件的建议,我将非常感激:) – anthony 2012-01-17 20:36:50
我不认为问题是没有下一行,问题是当我认为扫描仪没有得到所需的文件。在数组中它有单词scanner.in而不是我想要的文件。那是我需要帮助的。 – anthony 2012-01-17 20:34:25
的问题是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()
。
请通过Scanner
doc。
您的怀疑是对的:您没有打开文件,只是将字符串“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之前,可以让代码至少减少一点错误。
您的问题不清楚从您的问题陈述。是否有任何产出?或者是有输出产生,但它是错误的输出? – 2012-01-17 20:11:24
根本没有输出。它给我一个错误邮局主要说:线程“主”异常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
如果你可以编辑你的原始问题来包含错误代码以及一个合适的输入文件例子(几行将足够)。 – tim 2012-01-17 20:34:38