Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

code question  RSS feed

 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, what is wrong with my code here? I thought I can switch Jake with %d?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think something is wrong with it?

It helps others tremendously if you tell us what is happening along with what you expect.

Does this compile? does it run and throw an error? does it print something you didn't expect?
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some minor style errors, but Fred is right there is nothing wrong with the code. What do you mean by, “switch with %d”?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming the OP wants to use a format string. The correct method is printf().
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course that is it.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are just using "%d" without any field size specifications, don't use printf() at all; just use
Also, you should use the normal Java conventions and start all variable names with a lower case letter.
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:If you are just using "%d" without any field size specifications, don't use printf() at all;

Personally I think that's a bit too strict. When I want to embed multiple variables within a String, I think using printf() or String.format() is perfectly acceptable and can even be more clear than repeated concatenation.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I probably should have said is "when displaying a single variable value without field specs, you don't need to use printf or String.format; you can just use println."
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree. The real utility of printf for a single variable is that it allows you to print it at a predictable location on the screen; repeated use will give nice aligned columns.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!