gpt4 book ai didi

java 。读取输入时出现问题

转载 作者:行者123 更新时间:2023-12-01 16:36:06 25 4
gpt4 key购买 nike

我正在做一项涉及类和对象的家庭作业。在其中,我有一个 Address 类、Letter 类和 PostOffice 类,当调用 PostOffice 时,它​​们会获取输入文件并扫描它,基本上以信件上应有的方式打印输入文件中的所有内容(至:bla,来自: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)

我真的不明白为什么......

这是我的邮局类(class):

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 valueFrom Address value (be sure and use Address toString)                    To: To value                    To Address value (be sure and use Address toString)----------------------------------------------------------------------------

最佳答案

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

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

代码中的示例:

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

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

解决方案:

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

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

关于 java 。读取输入时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8901025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com