所以下面是四个类别中的一个(这个是主要的),我有正确的输出但是"交易簿列表#34;输出的一部分是打印三次。我相信这是因为循环,但我不知道如何解决它。我知道下面有很多代码,但我不确定你需要多少帮助我。代码的输出将低于代码,以便您更好地了解我的意思。
public class BookListGenerator {
//main
public static void main(String args[]) {
//declaring list to hold books
ArrayList<Textbook> textbooks = new ArrayList<>();
ArrayList<Tradebook> tradebooks = new ArrayList<>();
//Creates decimal format for output
DecimalFormat df = new DecimalFormat("00.00");
// reading file and creating objects
File fileName = new File("//Users//nbean207//Desktop//Books.txt");
try {
Scanner sc = new Scanner(fileName);
String type = "",textbook="",name="",ISBN = "",dataMember = "";
double price= 0;
int counter = 0;
while (sc.hasNextLine()) {
switch (counter) {
case 0:
type = sc.nextLine();
counter++;
break;
case 1:
textbook = sc.nextLine();
counter++;
break;
case 2:
name = sc.nextLine();
counter++;
break;
case 3:
ISBN = sc.nextLine();
counter++;
break;
case 4:
price = Double.parseDouble(sc.nextLine());
counter++;
break;
case 5:
dataMember = sc.nextLine();
counter++;
break;
}
if(counter == 6) {
counter = 0;
if(type.equals("Textbook")) {
Textbook obj = new Textbook(textbook, name, ISBN, price,dataMember);
textbooks.add(obj);
}
else{
Tradebook obj = new Tradebook(textbook, name, ISBN, price,dataMember);
tradebooks.add(obj);
}
}
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// reading user input
Scanner sc1 = new Scanner(System.in);
while(true) {
ArrayList<Textbook> tempTextbooks = new ArrayList<>();
ArrayList<Tradebook> tempTradebooks = new ArrayList<>();
System.out.println("Enter your major; ");
String major = sc1.nextLine();
while(true) {
System.out.println("Enter Course name: (xxx to quit): ");
String courseName = sc1.nextLine();
if(courseName.equals("xxx")) {
break;
}
for(int i=0;i<textbooks.size();i++) {
if(textbooks.get(i).dataMember.equals(courseName)) {
tempTextbooks.add(textbooks.get(i));
}
}
for(int j=0;j<tradebooks.size();j++) {
if(tradebooks.get(j).dataMember.equals(major)) {
tempTradebooks.add(tradebooks.get(j));
}
}
}
System.out.println("List of Textbooks: ");
double textbookprice = 0, tradebookprice = 0;
for(int i=0;i<tempTextbooks.size();i++) {
System.out.format("%-10s %-40s %-20s $%-15s \n", tempTextbooks.get(i).dataMember, tempTextbooks.get(i).title, tempTextbooks.get(i).author, df.format(tempTextbooks.get(i).getPrice()));
textbookprice = textbookprice + tempTextbooks.get(i).getPrice();
}
System.out.println("Sum of retail book prices: $"+df.format(textbookprice));
System.out.println("List of Tradebooks: ");
for(int j=0;j<tempTradebooks.size();j++) {
System.out.format("%-50s %-20s $%-15s \n",tempTradebooks.get(j).title, tempTradebooks.get(j).author, df.format(tempTradebooks.get(j).getPrice()));
tradebookprice = tradebookprice + tempTradebooks.get(j).getPrice();
}
break;
}
}
}
这是下面的输出:请注意,交易簿列表部分正在打印数据3次。这是我遇到的错误,我不确定如何修复它。
Enter your major;
COS
Enter Course name: (xxx to quit):
COS221
Enter Course name: (xxx to quit):
COS225
Enter Course name: (xxx to quit):
BUS398
Enter Course name: (xxx to quit):
xxx
List of Textbooks:
COS221 Introduction to Programming with C++ Y. Daniel Liang $120.99
COS225 Java Foundations John Lewis $94.59
BUS398 Excel 2010 Power Programming with VBA John Walkenbach $87.99
Sum of retail book prices: $303.57
List of Tradebooks:
C++ Primer Plus Stephen Prata $65.99
C++ Projects Programming with Text-Based Games Mike Dawson $54.95
Visual C++.Net Don Gosselin $76.99
Programming and Object Oriented Design Using Java Jaime Nino $98.99
Object Oriented Data Structures Nell Dale $116.59
C++ Primer Plus Stephen Prata $65.99
C++ Projects Programming with Text-Based Games Mike Dawson $54.95
Visual C++.Net Don Gosselin $76.99
Programming and Object Oriented Design Using Java Jaime Nino $98.99
Object Oriented Data Structures Nell Dale $116.59
C++ Primer Plus Stephen Prata $65.99
C++ Projects Programming with Text-Based Games Mike Dawson $54.95
Visual C++.Net Don Gosselin $76.99
Programming and Object Oriented Design Using Java Jaime Nino $98.99
Object Oriented Data Structures Nell Dale $116.59
答案 0 :(得分:0)
除了文本文件中的任何重复之外(此答案假设其中没有重复的条目):
您打印出商品手册:
for(int j=0;j<tradebooks.size();j++) {
if(tradebooks.get(j).dataMember.equals(major)) {
tempTradebooks.add(tradebooks.get(j));
}
}
在while循环中。
因此,每次有输入时都会打印
看一下评论代码:
while(true) {
System.out.println("Enter Course name: (xxx to quit): ");
String courseName = sc1.nextLine();
if(courseName.equals("xxx")) {
break;
}
for(int i=0;i<textbooks.size();i++) {
if(textbooks.get(i).dataMember.equals(courseName)) {
tempTextbooks.add(textbooks.get(i));
}
}
//Right here, every time you add a course, you also add tradebooks to the tempTextBooks-list. Meaning you add it three times.
for(int j=0;j<tradebooks.size();j++) {
if(tradebooks.get(j).dataMember.equals(major)) {
tempTradebooks.add(tradebooks.get(j));
}
}
}
您的问题的解决方案是在while循环之外移动交易的for循环