• 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

Learning Java - 0 Programing experience.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi JavaRanch cool people.

I just started learning Java and I am studying Head First Java. I'm doing the "Coding A Serious Business Application" on page 14. It's the classic Beersong.java activity that I have seen discussed on codeRanch.

Out of all of the threads I have read I have not seen anyone reporting the same issue I am seeing. I also am not understanding a fundamental piece of the code...

Question#1
My output looks like this...

"99 bottleof beer on the wall
99 bottleof beer."

Notice 'bottle' and 'of' are concatenated. I have reviewed my code many times and from everything I can tell it is verbatim what is listed on page 14 in headFirstJava.


Question#2
For this section of code "System.out.println (beerNum + " " + word + "of beer on the wall");" I do not understand why we need the " " between beerNum and word. I don't understand what purpose it plays as we assign "Bottles" to the String word variable so it seems redundant to me.

I hope this all makes sense.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) You are missing the space in front of the word 'of'.
2) If you don't understand why something is there, take it out and see what happens.
 
Joseph Kennedy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:1) You are missing the space in front of the word 'of'.
2) If you don't understand why something is there, take it out and see what happens.




"If you don't understand why something is there, take it out and see what happens."

Thank you for this priceless piece of advice. I will live by it. Also thank you for helping me with the "space" in front of "of". One of the first things they tell you in headFirstJava is that spacing rarely matters. Here is a time when it obviously does.

Thanks again!
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, spacing doesn't matter in Java, except: a) for human readability, and b) if it's enclosed in double quotes.

Glad to help

And welcome to The Ranch.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you don't understand why something is there, take it out and see what happens.



I've been working with Java for decades. I still write lots of small (<40 lines) programs just to test a language feature to make sure I really understand its behavior. You'll be doing a lot of this.
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:In general, spacing doesn't matter in Java, . . .

Maybe you should think that you need the spaces and indentation for your own purposes, but the compiler can usually cope without spacing.

As you have been told, what you have in the quoted text is a word without a preceding space. If you want the space displayed, you have to write a word with a preceding space.

And welcome to The Ranch.

Agree

Minor point: you may need spacing if you have two operators adjacent to each other, or a type adjacent to an identifier. You need to writeDon't read what follows
What happens if you combine the sign change operator and predecrement? Can the compiler cope with −−−i? That should expand to − −−i
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic