This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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...
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...
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.
Joined: Oct 08, 2012
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.
Joined: Oct 13, 2005
Well done sorting it out . Was the tutorial about text fields any good?
Joined: Oct 08, 2012
(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)
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!