2017-03-01 26 views
0

所以下面是四个类中的一个(这一个是主要的),并且我有正确的输出,但输出的“list of tradebooks”部分是打印三次。我相信这是因为一个循环,但我不知道如何解决它。我知道下面有很多代码,但我不确定你需要多少帮助。代码的输出将会在代码下面,以便更好地了解我的意思。Java输出打印太多,回路问题

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

同时粘贴'// Users // nbean207 //桌面// Books.txt'的内容。看起来你的文件包含多个条目。 – Maverick

+0

在阅读每个课程名称后,如果用户输入三次课程(三次打印出贸易书籍),则将与“major”相匹配的贸易书籍添加到“tempTradebooks”列表中 - 如果用户进入四门课程,您将打印出四本贸易书籍 –

+0

您的循环书不依赖于课程名称输入。将它从'while(true){}'循环中移出。 – hoaz

回答

0

从文本文件中的任何重复一边看(这个回答假设有在那里没有重复的条目):

您打印出tradebooks:

  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)); 
       } 
      } 
     } 

你的问题的解决方案是将for循环外tradebooks的while-loop