Java输出打印太多,循环问题

Java输出打印太多,循环问题,java,loops,Java,Loops,下面是四个类中的一个(这是主类),我有正确的输出,但是输出的“贸易书列表”部分打印了三次。我相信这是因为一个循环,但我不知道如何修复它。我知道下面有很多代码,但我不确定您需要多少帮助。代码的输出将在代码下面,以便更好地理解我的意思 public class BookListGenerator { //main public static void main(String args[]) { //declaring list to hold books

下面是四个类中的一个(这是主类),我有正确的输出,但是输出的“贸易书列表”部分打印了三次。我相信这是因为一个循环,但我不知道如何修复它。我知道下面有很多代码,但我不确定您需要多少帮助。代码的输出将在代码下面,以便更好地理解我的意思

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

除了文本文件中的任何重复(此答案假设其中没有重复条目):

您可以打印贸易手册:

           for(int j=0;j<tradebooks.size();j++) {
                if(tradebooks.get(j).dataMember.equals(major)) {
                    tempTradebooks.add(tradebooks.get(j));
                }
            }

for(int j=0;jAlso粘贴
//Users//nbean207//Desktop//Books.txt
的内容。看起来您的文件包含多个条目。在阅读每个课程名称后,您将与
专业
匹配的专业书籍添加到
trade Books
列表中-如果用户输入三门课程,您将执行三次(并将交易记录打印三次),如果用户输入四门课程,您将打印四次交易记录。您的交易记录for循环不依赖于课程名称输入。请将其从
while(true){}
循环中移出。@hoaz感谢您帮助我解决了这个问题,很抱歉问了这么长的问题。
           for(int j=0;j<tradebooks.size();j++) {
                if(tradebooks.get(j).dataMember.equals(major)) {
                    tempTradebooks.add(tradebooks.get(j));
                }
            }
        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));
                }
            }
        }