java n is printed, should break line? [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of java n is printed, should break line? [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have a string that when it see’s n it should break line. However when I print it too screen it actually shows the n as characters.

  1. My file has a line such as q1. what is your name? n a.bob n b.jim

  2. I then store that result into a String when its read from the file

  3. I then send it over RMI to a client which it is then displayed. It should be

    q1. what is your name?

    a. bob
    b. jim
    

However it all is printed on one line exactly how it is in the file.

code of it reading from file and returning back to client

    String [] line = new String [10];
            try 
            {
                int i =0;
                for (i=0;i<=9;i++)
                {
                    line[i] = "";
                }
                Scanner scanner = new Scanner(new File("questions.txt"));
                scanner.useDelimiter("Q");
                i=0;
                while (scanner.hasNext()) 
                {
                    line[i] = scanner.next();
                    //System.out.println("" + line[i]);
                    i++;
                }
            }
return line[0]; //return string back to client

Answer

n behaves like a control character only when you add it to a Java string yourself. When reading from the file the is read literally i.e. it gets escaped as \ and the following n is then interpreted as n only.

So, if you want the literal n to be interpreted as a newline by your client, you would have to replace it before return as

return line[0].replaceAll("\\n", "n");
We are here to answer your question about java n is printed, should break line? [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji