简体   繁体   中英

Fail to print on new line

EDIT: Failed to mention that it is a Java Web program and I think that's why the "\\n" wouldn't work, as I was checking my progress in the browser. It works now, used "<br/>" .

I'm writing a program that finds all lines from a CSV file, that contain a particular word and then prints those lines, however I cannot get it around to print them on new lines. Here is part of the code:

 try {
                in = new Scanner(file);
                while(in.hasNext())
                {
                    String line=in.nextLine();
                    if (line.contains(sb)) {
                        String newLine = System.getProperty("line.separator");
                        String[] parts = line.split(",");
                        String temp = "Name: "+parts[0]+" Price: "+parts[1] + newLine;
                        System.out.println(temp);
                        System.out.println(" ");
                    }
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

So, sb stands for the word that we use, which is taken from an input box, the program successfully prints the lines that contain the word, however if its more than 1 line, it prints it on the same line. Lets say that the word used is Howd , which finds those 2 lines:

Howd,33

Howd Pepper,44

Then, the output should be :

Name: Howd Price: 33

Name: Howd Pepper Price: 44

However I receive :

Name: Howd Price: 33 Name: Howd Pepper Price: 44

and I cannot get it to print on a new line. I tried adding an empty println,"\\n" and line.seperator, is something interfering with the code? What am I doing wrong? Thanks!

I failed to mention that it is a java web program.

But I did manage to do it with +"<br/>" to the end of the string, so it works now.

Not sure if I can close the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM