aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Why can't I get \n to work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why can Watch "Why can New topic
Author

Why can't I get \n to work?

Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
OK, I'm learning Java and following a tutorial which I can get 99% working aside from the use of \n to split output onto more than one line.

The tutorial is all about manipulating Java Forms with check boxes etc. etc. So I have 4 check boxes a user can select any number of, a button which when clicked will display what the user has checked in a separate text field. As I said it all works except for the fact that the details of which boxes have been checked get churned out without line breaks.

Here's the code associated with the button, as you'll spot, \n is supposed to add a line break after each of the checkboxes that have been checked...



Your help is appreciated, my thanks.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

If you could read about escape sequence, then it doesn't happen with you.

http://docs.oracle.com/javase/tutorial/java/data/characters.html

-hth


OCA7
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
Well thanks @ankita but I'm not sure what you're telling me. I understand that \n is an escape sequence that adds a line break to output and I have had it working when outputting to the console with other tutorials. In this instance the output is going to a text field on a Java form and the \n is not working so my output looks like (assuming 4 checkboxes have been selected)...

Checkbox1 Name Checkbox2 Name Checkbox3 Name Checkbox4 Name

Instead of the hoped for output of...

Checkbox1 Name
Checkbox2 Name
Checkbox3 Name
Checkbox4 Name

My thanks
Tarun Trehan
Ranch Hand

Joined: Mar 16, 2012
Posts: 48

Try using "\\n" .

Thanks,
Tarun Trehan (http://allzhere.in)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

I'm not sure, and this is a WAG, but perhaps you want to append it as a string, not a character? i.e.

note the double quotes


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
Well thanks @Tarun and @fred.

I tried both methods (sadly neither worked).

With the "\\n" idea from Tarun I got...

Checkbox1 Name\n Checkbox2 Name\n Checkbox3 Name\n Checkbox4 Name\n

With @fred's suggestion of "\n" (instead of '\n') I got the same as my earlier post i.e.

Checkbox1 Name Checkbox2 Name Checkbox3 Name Checkbox4 Name

Thanks for your interest though.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Actually, you ought not to use \n which is specific to Unix (and Linux). Macs used to use \r for line end but have changed to \n a few years ago. Windows uses both characters \r\n. Many apps can detect and compensate for the line end, but a few (eg MS Notepad) can’t.
Try String#format and the %n tag. You can find more about % tags here, here, and if you want all the details, here.

I do not know whether a text field actually supports multiple lines; you would have to look in the documentation. That documentation may have a tutorial link, which is worth following.
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
Whoops, sorry for my delayed reply but thought that I'd let you know that @Campbell's post was the breakthrough. Sadly Text Fields don't support multi lines but Text Areas do. So I changed my form to incorporate a Text Area and hey presto all is fine.

My thanks to all for your support. Good to know that there are guys out there willing to help newbies like myself.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Well done sorting it out . Was the tutorial about text fields any good?
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
(Bet you wish you hadn't asked whether the tutorials were any good!) Well yes, I'd have to say that the whole series of tutorials that I've now fully completed has been a pretty good intro to Java programming - here's the link to the tutorials.

Why do I rate the tutorials?

Well my first mistake was to dive in too deeply. I bought myself an Android Development book (Beginning Android 4 Application Development), struggled and got to about page 70, kept making errors that prevented my test apps from compiling and realised that I needed to take a step backwards and learn something about Java.

Next step was not quite as frought but still had issues in that I bought the book 'Head First Java'. Now don't get me wrong, this seems like a really good book but the previous problems I'd had with Android which were mostly about setting up projects / packages / main classes etc. etc. started to recur so I felt I needed to do a real beginner's set of tutorials and that's where I found the link in my first para.

Funny as someone else posted a "Where do I start to learn Java" thread yesterday (link here) which I found useful so, having finished the tutorials I mention, I'm now off...

  • Reading a book called 'Introduction to Programming using Java'. (link here)
  • Reviewing another set of tutorials here
  • And generally scouring the web for Java learning opportunities


  • FYI. Prior to the tutorials I mention in my first para, I spent at least 1 month going round and around in circles on Android / Java etc. etc. I don't feel out of the woods on all this yet but feel as though I'm starting to understand some of the basics.

    That's why I thanked you guys earlier. It's a stuggle down here at the bottom of the heap and, personally, I appreciate all the help you guys are willing to offer!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Why can't I get \n to work?
     
    Similar Threads
    what's wrong with my jtree code ?
    Issue in Displaying panel
    Split Pane not working
    JCheckbox group
    Solved : Java CheckBox to insert data