• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why can't I get \n to work?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using "\\n" .
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out . Was the tutorial about text fields any good?
 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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!
     
    Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic