Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotePad not recognizing new line characters in java code using PrintStream to output to text file

Tags:

java

I need some help with my java project for school. Please see the code below for the classes Book and BookApplication, as well as the input text file and desired output text file.

I'm having an issue with the output file. When I open it in Word or WordPad or NotePad++, the file is formatted correctly. But when I open the file in NotePad, the report is all on one line.

For some reason NotePad is not recognizing the new line characters. Any thoughts on this? How can I change the java code so that NotePad displays the report correctly? I am a Java noob and I'm really struggling in this Java course. Any help you can offer would be greatly apprecited. Thanks.

The input text file called inBook.txt reads as follows:

Intro to Java 0123444555680 Tony Gaddis TG003 Intro to Javascript 9780071632969 John Pollock JP402 Fundamentals of Game Development 9780763778 Heather Maxwell Chandler HC026

The output text file called outBook.txt needs to read exactly as follows:

Dean Publishers - List of Books

Intro to Java By Tony Gaddis (TG003) ISBN: 0123444555680

Intro to Javascript By John Pollock (JP402) ISBN: 9780071632969

Fundamentals of Game Development By Heather Maxwell Chandler (HC026) ISBN: 9780763778

Our current catalog has 3 books.

Thank you for your interest.

// Book.java

public class Book {

    public static final String BOOK_PUBLISHER = "Dean Publishers";

    private String bookTitle;
    private String bookISBN;
    private Author bookAuthor;

    Book(String inTitle) {
        setBookTitle(inTitle);
        setBookISBN("0");
        setBookAuthor("");
    }

    public void setBookTitle(String inTitle) {
        bookTitle = inTitle;
    }

    public void setBookISBN(String inISBN) {
        bookISBN = inISBN;
    }

    public void setBookAuthor(String inName) {
        bookAuthor = new Author(inName);
    }

    public void setBookAuthorID(String inID) {
        bookAuthor.setAuthorID(inID);
    }

    public String getBookTitle() {
        return bookTitle;
    }

    public String getBookISBN() {
        return bookISBN;
    }

    public Author getAuthor() {
        return bookAuthor;
    }

    public String getAuthorName() {
        return bookAuthor.getAuthorName();
    }

    public String getAuthorID() {
        return bookAuthor.getAuthorID();
    }

    //inner class
    class Author {
        private String authorName; //instance property
        private String authorID; //instance property

        //constructor
        Author(String inName)  {
            setAuthorName(inName);
            setAuthorID("0");
        }

        public void setAuthorName(String inName) {
            authorName = inName;
        }

        public void setAuthorID(String inID) {
            authorID = inID;
        }

        public String getAuthorName() {
            return authorName;
        }

        public String getAuthorID() {
            return authorID;
        }
    } //end inner Author class

} //end Book class

BookApplication.java

import java.util.*;
import java.io.*;

public class BookApplication {

    public static void main(String[] args) throws IOException {
        // Create Book object for each book in the file and store them in array.
        Scanner filein = new Scanner(new File("inBook.txt"));

        // I will use ArrayList, a dynamic-size array
        ArrayList<Book> books = new ArrayList<Book>();

        while(filein.hasNextLine()) {
              // read book data
              String title = filein.nextLine();
              String isbn = filein.nextLine();
              String author = filein.nextLine();
              String authorID = filein.nextLine();

              // create new book from input
              Book temp = new Book(title);
              temp.setBookISBN(isbn);
              temp.setBookAuthor(author);
              temp.getAuthor().setAuthorID(authorID);

              // add to arraylist
              books.add(temp);
        }

        // Create an output report by reading the Book objects from the array.
        String output = "";
        for(Book book : books) {
           output += book.getBookTitle() + "\n";
           output += "By "+book.getAuthor().getAuthorName() + " (" + book.getAuthor().getAuthorID() + ")\n";
           output += "ISBN: " + book.getBookISBN() + "\n\n";
        }

        // Report prints to console
        System.out.println(output);

        // Report prints to file called outBook.txt
        PrintStream fileout = new PrintStream(new File("outBook.txt"));
        fileout.println(output);
        fileout.close();

        // Count the number of books outputted to the report.
        System.out.println("Our current catalog has " + books.size() + " books.");
        System.out.println("\nThank you for your interest.");

    } // end main method
} // end BookApplication
like image 751
user644996 Avatar asked Apr 05 '11 20:04

user644996


People also ask

How do you go to a new line in Java?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.


2 Answers

You are using the \n character for new lines, which is correct for *nix. In windows it's \r\n. If you don't want to worry about what platform you're on, you can use:

System.getProperty("line.separator")

To get that token. That way your program will work on any platform.

Update: It looks like in Java 7 you can just use System.lineSeparator().

like image 135
Kirk Woll Avatar answered Sep 30 '22 16:09

Kirk Woll


You need \r\n (0x0D 0x0A) on Windows to end a line, and two pairs of them to output a blank line.

like image 25
Ken White Avatar answered Sep 30 '22 16:09

Ken White