Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:43:59+00:00 2026-06-11T16:43:59+00:00

In the following code, case 2 prints the text file in normal order, it

  • 0

In the following code, case 2 prints the text file in normal order, it works fine. But in case 3, the exact same thing, but using the method .reverse(), it adds line breaks out of nowhere. (I know I can cut out a lot of repetition, I’m trying to get it working first.)

Example:

Marcus
1244

looks like

4421

scuraM

using \r or \n instead of line.separator gives me the same exact thing. taking it away, of course, gives me one smashed together line.

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

public class H5 {
public static void main(String args[]) {
    Scanner stdin = new Scanner(System.in);
    Scanner stdin2 = new Scanner(System.in);
    String filePath = null;
    int selection;
    boolean repeat = true;
    FileInputStream f = null;
    
    do {  
        System.out.println("\n0 - Exit\n1 - Select file\n2 - Display\n3 - Reverse\nSelect option: ");
        selection = stdin.nextInt();
        switch (selection) {
        case 0:  System.out.println("\nThank you. Goodbye.");
                 repeat = false;
                 break;
        case 1:  System.out.println("\nFile path: ");
                 filePath = stdin2.nextLine();
                 
                 try {f = new FileInputStream(filePath);}
                 catch (Exception d) { System.out.println(d);}
                 
                 break;
        case 2:  try {
                    f = new FileInputStream(filePath);
                    DataInputStream d = new DataInputStream(f);
                    BufferedReader b = new BufferedReader(new InputStreamReader(d));
                    StringBuffer strbuf = new StringBuffer(200000);

                    String strLine;
                    while ((strLine = b.readLine()) != null) {
                         strbuf.append(strLine).append(System.getProperty("line.separator"));
                     }
                    System.out.println(strbuf);
                 }
                 catch(NullPointerException npe) {
                    System.out.println("\nPlease select a file first.");
                 }
                 catch(Exception e) {
                    System.out.println(e);
                 }
                 break;
       case 3:  try {
                    f = new FileInputStream(filePath);
                    DataInputStream d = new DataInputStream(f);
                    BufferedReader b = new BufferedReader(new InputStreamReader(d));
                    StringBuffer strbuf = new StringBuffer(200000);

                    String strLine;
                    while ((strLine = b.readLine()) != null) {
                         strbuf.append(strLine).append(System.getProperty("line.separator"));
                     }
                    strbuf.reverse();
                    System.out.println(strbuf);
                 }
                 catch(Exception k) {
                     System.out.println(k);
                 }
                 break;
        default: System.out.println("\nInvalid input. Please select from the following: ");
                 break;
        }
    } while(repeat);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:44:00+00:00Added an answer on June 11, 2026 at 4:44 pm

    That’s because \r\n is a new line, but \n\r is two new lines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following Code works fine for an admin account but for an non admin
I have the following code (in this case for two images - but in
I want the following code to print 11, but prints 12, except in the
The following code is simplified case for my Java2D application which prints to PDF.
I wrote the following code to copy the text file to another: FILE *fpr,
Is the following code the case of legal forward referencing? if yes why? public
The following code looks at any element with the title-case class and modifies the
I have code like following: switch(sort.Column) { case code: model = (sort.Direction == SortDirection.Ascending)
I have the following code in test.sh: while getopts f:i: opt; do case $opt
I have the following code: .h NSString *mainString; .m case 0: case 1: case

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.