• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays syntax  RSS feed

 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my book I have to create this code,


Which is not very complicated, but I have a different result on the str - println, where the answer page reads "Much Better Java", my code generates "MuchBetterJava" which has no spaces, I'm wondering have I done something to create this bunching of words, or have I left out a segment of code that will allow spaces to be generated without having to use +" "+ for each word.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the book wants you to add +" "+.

It can be done without that concatentation, but that uses more advanced methods that were added in Java 8. To give you a taste of what you'll learn at some point, this prints it with the spaces in one line.


Don't worry that you don't understand the example yet. Proceed with + " " + and you'll get up to the advanced way in the future. Plus, the way without having to concatentate isn't even shorter!
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeanne,

I thought that was going to be the case. On the instruction pages the method printed in the book doesn't include the +" "+ between them,
and I have never been asked to write something that wasn't clearly instructed before, so I assumed there was further learning to do.

If these are my only two options, then I will have to wait until I advance a little more.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All sorts of ways to do it. You can find out about the StringJoiner class, which will do just about the same as Jeanne has told you.
You can use + " " + as Jeanne told you too.
I would suggest
System.out.printf("%s %s %s%n", str[0], str[1], str[2]);
Note the spaces between the successive %s tags.
If you are not familiar with the % tags, be sure to ask.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, always tell us which book or other source it is.
 
Vincent Tyson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cambell,

I'm not at the computer right now but I'll try your method later on and see how it goes for me.

I wasn't sure if posting the books name would be allowed due to advertising restrictions so I purposely left it out.

I'm using Java in easy steps by Mike McGrath third edition for SE7. It's easy enough to follow but I dont have a tutor to ask questions too. So the more complex statements leave me a bit uneasy. All in all learning java is a twisted frustrated fun. So far it's been enjoyable except for the nightmares lol.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent Tyson wrote: . . . So far it's been enjoyable except for the nightmares lol.
I am afraid that is normal :wink:

Thank you for telling us the book's name.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!