Выводить только частичный результат с помощью PrintStream

Пожалуйста, взгляните на мой код.

import java.util.*;
import java.io.*;
public class LibraryDriver {
    public static void main(String[] theArgs) {
        String theAuthor = "";
        String theTitle = "";
        Scanner input = null;
        PrintStream output = null;
        try {
            input = new Scanner(new File("LibraryIn1.txt"));
            output = new PrintStream(new File("LibraryOut.txt"));
        } catch (Exception e) {
            System.out.println("Difficulties opening the file! " + e);
            System.exit(1);
        }
        ArrayList < String > authors = new ArrayList < String > ();
        ArrayList < Book > books = new ArrayList < Book > ();
        while (input.hasNext()) {
            // Read title 
            theTitle = input.nextLine();
            // Read author(s)
            theAuthor = input.nextLine();
            authors = new ArrayList < String > (getAuthors(theAuthor));
            // Insert title & author(s)into a book

            // Add this book to the ArrayList<Book> of books 
            books.add(new Book(theTitle, authors));
            authors.clear();
        }

        // Instantiate a Library object filled with the books read thus far 
        // and write the contents of the library to the output file 
        Library lib = new Library(books);
        output.println("PRINTS INITIAL BOOK LIST:");
        output.println(lib);
        // Sort the current contents of the library
        lib.sort();
        // and write the contents of the sorted library to the output file
        output.println("\nPRINTS SORTED BOOK LIST:");
        output.println(lib);
        // Close the first input file and open the second input file. 
        // Read the titles and authors from the second input file, 
        // add them to the library, and write the contents of the 
        // library to the output file. 
        input.close();

        try {
            input = new Scanner(new File("LibraryIn2.txt"));
            output = new PrintStream(new File("LibraryOut.txt"));
        } catch (Exception e) {
            System.out.println("Difficulties opening the file! " + e);
            System.exit(1);
        }
        while (input.hasNext()) {
            theTitle = input.nextLine();
            theAuthor = input.nextLine();
            authors = (getAuthors(theAuthor));
            Book b = new Book(theTitle, authors);
            lib.add(b);
        }
        output.println("\nPRINT WITH NEW BOOK UNSORTED:");
        output.println(lib);
        // Sort the library and write it to the output file 
        lib.sort();
        output.println("\nPRINT ALL SORTED BOOK LIST:");
        output.println(lib);
        // The following tests the findTitles method, i.e. test
        // the findTitles method by passing “Acer Dumpling” and
        // then “The Bluff”:
        // Write only the "Acer Dumpling" books to the output file
        output.println("\nPRINT ALL ACER DUMPLINGS:");
        for (Book b: lib.findTitles("Acer Dumpling")) {
            output.println(b);
        }
        // Write only the "The Bluff" books to the output file
        output.println("\nPRINT ALL THE BLUFFS:");
        for (Book b: lib.findTitles("The Bluff")) {
            output.println(b);
        }

        // Close all open files and end main. 
        input.close();
        output.close();
    }

    // Header for method that separates author names and 
    // returns an ArrayList<String> containing the author names 

    public static ArrayList < String > getAuthors(String theAuthors) {
        String[] temp = theAuthors.split("\\*");
        ArrayList < String > result = new ArrayList < String > (Arrays.asList(temp));
        return result;
    }
}

После запуска этой программы выходной файл загружается только так:

PRINT WITH NEW BOOK UNSORTED:
(the list of books' title and authors)

PRINT ALL SORTED BOOK LIST:
(the list of books' title and authors)

PRINT ALL ACER DUMPLINGS:
(the list of title with acer dumpling)

PRINT ALL THE BLUFFS:
(the list of title with the bluff)

Первые две части «ПЕЧАТЬ НАЧАЛЬНОГО СПИСКА КНИГ» и «ПЕЧАТЬ СПИСКА СОРТИРОВКИ КНИГ» отсутствуют, но я не знаю, как это понять. Спасибо!


person DONGYANG TAO    schedule 05.11.2019    source источник


Ответы (1)


Вы спросили, не знаете, как это понять.

Мой ответ упоминается ниже:

  1. Импортируйте свой проект в файл eclipse.
  2. Поместите точку отладки в свой код в несколько точек.
  3. Используйте значок ошибки в eclipse для отладки файла класса.
  4. Как только он достигнет установленного вами указателя отладки, используйте F6 для отладки построчно или вы можете использовать F5, чтобы перейти к методу.

Вы также можете обратиться по ссылке, указанной ниже:

https://www.eclipse.org/community/eclipse_newsletter/2017/june/article1.php

person marco525    schedule 05.11.2019
comment
Я выполнил отладку с помощью jgrasp, программа проходит через эти две части, но в выходном файле по-прежнему отсутствуют первые две части. Поскольку я пытался использовать только System.out.println для них, эти две части загружаются в мой блок ввода-вывода запуска. - person DONGYANG TAO; 06.11.2019
comment
Можете ли вы также поделиться уроками книги и библиотеки? - person marco525; 06.11.2019