Note that there are some explanatory texts on larger screens.

plurals
  1. PO\n Not working when reading from File to List and then to output. Java
    text
    copied!<p>I have a minor problem, the <code>\n</code>'s in my file isn't working in my output I tried two methods:</p> <p><strong>PLEASE NOTE:</strong></p> <p>*The text in the file here is a much simplified example. That is why I do not just use the <code>output.append("\n\n");</code> in the second method. Also the <code>\n</code>s in the file are not always at the END of the line i.e. a line n the file could be <code>Stipulation 1.1\nUnder this Stipulation...etc</code>. *</p> <p><em>The <code>\n</code>'s in the file need to work. Also both <code>JOptionPane.showMessageDialog(null,rules);</code> and <code>System.out.println(rules);</code> give the same formatted output</em></p> <p><strong>Text in File:</strong></p> <pre><code>A\n B\n C\n D\n </code></pre> <p><strong>Method 1:</strong></p> <pre><code> private static void setGameRules(File f) throws FileNotFoundException, IOException { rules = Files.readAllLines(f.toPath(), Charset.defaultCharset()); JOptionPane.showMessageDialog(null,rules); } </code></pre> <p><strong>Output 1:</strong></p> <pre><code>A\nB\nC\nD\n </code></pre> <hr> <hr> <p><strong>Method 2:</strong></p> <pre><code> private static void setGameRules(File f) throws FileNotFoundException, IOException { rules = Files.readAllLines(f.toPath(), Charset.defaultCharset()); StringBuilder output = new StringBuilder(); for (String s : rules) { output.append(s); output.append("\n\n");//these \n work but the ones in my file do not } System.out.println(output); } </code></pre> <p><strong>Output 2:</strong></p> <pre><code>A\n B\n C\n D\n </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload