how to insert a new line character in a string to PrintStream then use a scanner to re-read the file

183,248

The linefeed character \n is not the line separator in certain operating systems (such as windows, where it's "\r\n") - my suggestion is that you use \r\n instead, then it'll both see the line-break with only \n and \r\n, I've never had any problems using it.

Also, you should look into using a StringBuilder instead of concatenating the String in the while-loop at BookCatalog.toString(), it is a lot more effective. For instance:

public String toString() {
        BookNode current = front;
        StringBuilder sb = new StringBuilder();
        while (current!=null){
            sb.append(current.getData().toString()+"\r\n ");
            current = current.getNext();
        }
        return sb.toString();
}
Share:
183,248
Sara
Author by

Sara

Updated on July 21, 2020

Comments

  • Sara
    Sara almost 4 years

    I have several classes designed to simulation a book catalog. I have a book class (isbn, title, etc...), a BookNode class, a BookCatalog which is a LinkedList of books and a driver class (gui). My problem is that I have a toString() method in BookCatalog that supposed to return a String representation of all the books. The Book class also overrides toString(). I'm supposed to have each field of the book separated by a "tab" and each book separated by a "new line". When I try to use PrintStream to print the book catalog to a .txt file, the \n doesn't register.

    I've tried to change it to System.getProperty(line.separator) which displays the bookcatalog correctly. But now, I have a problem where the Scanner will not read the file correctly and throws a "NoSuchElementException". How do I get the scanner to 1) Ignore the line.separator or 2) have printStream use \n?

    Book.java

    public String toString(){
            return isbn+"\t"+lastName+"\t"+firstName+"\t"+title+"\t"+year+"\t"+
                String.format("%.2f",price);
    

    BookCatalog.java

    public String toString() {
            BookNode current = front;
            String s="";
            System.out.println(s);
            while (current!=null){
                //each book is listed on separate line
                s+=current.getData().toString()+"\n ";//System.getProperty("line.separator")
                current = current.getNext();
            }
            return s;
        }
    

    Driver.java

    public void loadDirectory() throws FileNotFoundException {
            if (f.exists()){
                Scanner input = new Scanner(f);
                while (input.hasNextLine()){
                    String bookLine = input.nextLine();
                    processBookLine(bookLine);
                }
            }
        }
    
    public void processBookLine(String line){
            Scanner input = new Scanner(line);
            String isbn = input.next();
            String lastName = input.next();
            String firstName = input.next();
    
            String title = input.next();
            while (input.hasNext() && !input.hasNextInt()){//while next token is not an integer
                title += " "+input.next();
            }
            int year = input.nextInt();
            double price = input.nextDouble();
            Book book = Book.createBook(isbn, lastName, firstName, title, year, price);
            if (book!=null){
                catalog.add(book);
            }
        }
    
    • user207421
      user207421 about 11 years
      If you're claiming that PrintStream suppresses the \r you are mistaken. The remainder of your question is not clear.
    • Sara
      Sara about 11 years
      I used the System.getProperty("line.separator") instead of \n so when i open the newly created file (from PrintStream) and tried to reread it (scanner), it will not process the line. I'm assuming that the "line.separator" is the reason why so I tried to save the character in another variable to bypass it but that didn't work either.
  • user207421
    user207421 about 11 years
    I don't see any reason why this should make any difference. Scanner handles \n, \r, and \r\n as line terminators already, and several others as well.
  • ddmps
    ddmps about 10 years
    @EJP Scanner yes, but if you open a .txt on notepad with only \n in Windows, it will not register.
  • AVA
    AVA over 8 years
    @ddmps It inserts an extra line at the end! Please suggest how to handle it!
  • ddmps
    ddmps over 8 years
    @AVA I do not see the issue with the "extra" line. See stackoverflow.com/a/729795/1690982.
  • AVA
    AVA over 8 years
    @ddmps Thanks for clarification with reference!
  • iamjoshua
    iamjoshua over 6 years
    Using "\r\n" instead of "\n" work just fine for me. Thanks alot. Upped!.