0

I have a minor problem, the \n's in my file isn't working in my output I tried two methods:

PLEASE NOTE:

*The text in the file here is a much simplified example. That is why I do not just use the output.append("\n\n"); in the second method. Also the \ns in the file are not always at the END of the line i.e. a line n the file could be Stipulation 1.1\nUnder this Stipulation...etc. *

The \n's in the file need to work. Also both JOptionPane.showMessageDialog(null,rules); and System.out.println(rules); give the same formatted output

Text in File:

A\n
B\n
C\n
D\n

Method 1:

 private static void setGameRules(File f) throws FileNotFoundException, IOException 
    {
        rules = Files.readAllLines(f.toPath(), Charset.defaultCharset());     
        JOptionPane.showMessageDialog(null,rules);
    }

Output 1:

A\nB\nC\nD\n


Method 2:

 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);
    }

Output 2:

A\n
B\n
C\n
D\n
tshepang
  • 12,111
  • 21
  • 91
  • 136
Dinocv
  • 367
  • 2
  • 3
  • 10
  • Building on StormHawke's comment (which is now gone...), just do something like `s = s.replaceAll( "\\\\n", System.lineSeparator() );` – Ray Stojonic Oct 04 '13 at 18:57
  • @RayStojonic but this will not help, if he wants to display this in a JOptionPane, will it? – Carsten Hoffmann Oct 04 '13 at 19:04
  • @RayStojonic that worked :\ but not fully, If I am generating output for the entire of `rules` then its quick and easy. But I need to call the rules individually and the `\n` needs to work in that scenario without having to replace the `\n` with a `lineSeperator` every time – Dinocv Oct 04 '13 at 19:43

3 Answers3

1

What do you mean with "it is not working"? In what way are they not working? Do you expect to see a line break? I am not sure if you actually have the characters '\n' at the end of each line, or the LineFeed Character (0x0A). The reason your '\n' would work in the Javas source is, that this is a way to escape the linefeed character. Tell us a little about your input file, how is it generated?

Second thing I notice is, that you print the text to the console in the second Method. I am not certain, that the JOptionPane will even display line breaks this way. I think it uses a JLabel, see Java: Linebreaks in JLabels? for that. The console does interpret \n as a linebreak.

Community
  • 1
  • 1
Carsten Hoffmann
  • 901
  • 5
  • 14
  • I'll update my question with the information requested. But just to touch on it: I've used both `JOptionPane` and `println` to the exact same effect. `println` does recognize the `\n` that I `append`. There are `\n`s in my source file that the output needs to obey – Dinocv Oct 04 '13 at 19:07
1

The character sequence \n is simply a human readable representation of an unprintable character.

When reading it from a file, you get two characters a '\' and an 'n', not the line break character.

As such, you'll need to replace the placeholders in your file with a 'real' line break character.

Using the method I mentioned earlier: s = s.replaceAll( "\\\\n", System.lineSeparator() ); is one way, I'm sure there are others.

Perhaps in readAllLines you can add add the above line of code to do the replacement before, or as, you stick the line in the rules array.

Edit:

The reason this doesn't work the way you expect is because you're reading it from a file. If it was hardcoded into your class, the compiler would see the '\n' sequence and say "Oh boy! A line separator! I'll just replace that with (char)0x0A".

Ray Stojonic
  • 1,260
  • 1
  • 7
  • 11
0

The final Answer looks like this:

private static void setGameRules(File f) throws FileNotFoundException, IOException {
        rules = Files.readAllLines(f.toPath(), Charset.defaultCharset());
        for(int i =0;i!=rules.size();i++){
            rules.set(i, rules.get(i).replaceAll( "\\\\n","\n"));
        }
    }

As @Ray said the \n in the file was just being read as chars \ and n not as the line seperator \n I just added a for-loop to run through the list and replace them using:

rules.set(i, rules.get(i).replaceAll( "\\\\n","\n")

Dinocv
  • 367
  • 2
  • 3
  • 10