【问题标题】:Only output partial result with PrintStream仅使用 PrintStream 输出部分结果
【发布时间】:2019-11-05 06:01:46
【问题描述】:

请看一下我的代码。

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)

前两部分“PRINT INITIAL BOOK LIST”和“PRINT SORTED BOOK LIST”缺失,但我不知道如何弄清楚。 谢谢!

【问题讨论】:

    标签: java printstream


    【解决方案1】:

    你问了不知道怎么弄出来。

    我的回答如下:

    1. 在 Eclipse 中导入您的项目。
    2. 将调试点放在代码中的多个位置。
    3. 使用eclipse中的bug图标调试类文件。
    4. 一旦到达您设置的调试指针,使用 F6 逐行调试,或者您可以 使用 F5 跳转到方法。

    您也可以参考下面提到的链接:

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

    【讨论】:

    • 我使用 jgrasp 进行了调试,程序通过了这两个部分,但输出文件仍然缺少前两个部分。当我试图只为那些使用 System.out.println 时,这两部分会加载到我的运行 I/O 块上。
    • 你能分享一下图书和图书馆的课程吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多