0

This may be an obvious answer to some of you, but I am trying to print out tables of data using string formatting. However, whenever I use \n the code isn't printed on a new line in browser, but it is printed correctly to terminal using System.out.println. This is basically what I have:

package com.example.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Test {

    public static void main(String[] args) {
        SpringApplication.run(Test.class, args);
    }

    @GetMapping("/problem")
    public String problem(@RequestParam(value = "no", defaultValue = "0") int no){
        String ret = "";

        switch(no){
            case 0:
                ret += "all problems";
                break;
            case 1:
                ret += ("Problem \n" +
                         "1");
                break;
            default:
                break;

        }
        System.out.println(ret);
        return ret;
    }

}

I would like for "Problem" and "1" to print on separate lines, just as an example. Please let me know what I'm doing wrong, or if this is totally off-base then I'd like to know what I should look into.

Thanks!

teatep
  • 1
  • 1
  • Typically you'd use something like HTML to instruct the browser your text should be rendered as is. https://stackoverflow.com/questions/588356/why-does-the-browser-renders-a-newline-as-space#588362 – M.P. Korstanje Mar 27 '21 at 23:52
  • I'll look into that, thank you!! @M.P.Korstanje – teatep Mar 28 '21 at 00:22

1 Answers1

2

Try this:

            switch(no){
                case 0:
                    ret += "all problems";
                    break;
                case 1:
                    ret += ("Problem <br>" +
                            "1");
                    break;
                default:
                    break;

            }

Working in my system