• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Just a bit of advice on my first program.

 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All, Well, I have been studying Java for just over a month and I have almost finished my first program. But as you see I am stuck. Below is a copy of the code for a program I wrote MANY years ago in Tandy Basic, written in Java. I don't understand why my System.out.println call won't work. A word of why would help. I don't want you to fix it just let me know how to fix it myself. Any help would be great. Here is the code:

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

The println() method takes just one argument. In Java, you can create longer strings out of shorter pieces using the "+" operator, so

"Number of Hens = " + hens

assembles the two pieces "Number of Hens =" and the contents of the variable 'hens' together into one long String. Then you can pass that String to println():

System.out.println("Number Of Hens = " + hens);

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please UseCodeTags when you post source code.
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the replies. I do understand about the code formating just didn't know how to do it thanks.

Well, I finished the changes and ran the program and IT WORKED! Thanks again for the pointers.

Here is the repaired code so ppl can see what i did.


 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Humphreys wrote:I don't understand why my System.out.println call won't work.

Glad you were able to get your code to work.

Another tip for posts around here....generally, saying "X doesn't work" doesn't help anyone understand your problem. It could mean anything from 'javac is not a recognized program or executable" to classpath issues to syntax errors to logic errors to any of about a billion other things.

Your job, when posting here, is to make it as easy as possible for folks to help you. If you get a syntax error when compiling, post the exact and complete text of that error. If the results are different than what you expect when you run it, tell us what you got AND what you expected.

people here LOVE to help others. But, if it comes to a choice between helping someone who has made it easy to help them and someone who made it hard...well...you can guess which most folks would choose.
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OP crossposted this topic here.

OP- Please BeForthrightWhenCrossPostingToOtherSites. I feel like I wasted my time on the other forum simply repeating help you already received, and will probably not be helping you in the future.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49370
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Kevin Workman.

But are you aware of the PrintStream#printf(java.lang.String,%20java.lang.Object...) method, or the Formatter class. They will allow you to format output. System.out is a PrintStream, so you can write System.out.printf( . . . ).
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all of your responses. I have assimilated your suggestions and will take action to implement same in a rapid fashion. As for Campbell Ritchie and Kevin Workman I am sorry that I stepped on your toes so to speak. I was not aware of the requirement and will surely not crosspost in the future, but do you not think it just a bit harsh that after one infraction and the poster is told he will receive no more help? Thank you all for your help
allen1952
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no official policy about not helping crossposters; it's just that Kevin feels his effort helping you on another site was wasted, and won't personally want to help you again. A lot of people will feel that way.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
It's not even that cross posting on another site is bad. The idea is to provide a link to the cross post in each place. That way someone can see if your issue has been solved before posting.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49370
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Humphreys wrote: . . . I am sorry that I stepped on your toes so to speak. . . .
Sorry for not replying earlier. Apologies accepted.
I won't go so far as not to help in future, myself.
 
Michael Humphreys
Greenhorn
Posts: 23
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Michael Humphreys wrote: . . . I am sorry that I stepped on your toes so to speak. . . .
Sorry for not replying earlier. Apologies accepted.
I won't go so far as not to help in future, myself.


Thank you Campbell, I do have a better understanding of what cross-posting is and will avoid it at all cost (smiles). Thank you all for your responses.
Michael
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic