• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with printf statement  RSS feed

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just need to know what I am doing wrong, please advise thank you.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

From the API docs for the printf method (emphasis mine)
printf(String format, Object... args)

Now can you see whats going wrong?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't use \n least of all in printf. Use %n for the correct line end. Only use \n if you have been told to produce an LF character.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And don't use \n least of all in printf. Use %n for the correct line end. Only use \n if you have been told to produce an LF character.


What is a LF character?
 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different operating systems handle the invisible end of line characters differently. So if you hard code a particular end of line that works on your system, it might behave differently (possibly incorrectly) when someone on a different operating system uses it. Not too much of an issue if you are just learning or only develop code for your personal use, but it is a good practice to get into. Have a quick search for 'windows unix end of line' or some variation of that and you will see.

Hope it helps,

Stu
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The more you look, the more complicated it becomes. Have a look at this Wikipedia page, and I shall be very surprised if that doesn't confuse you completely
But only use \n if you have been told to produce an LF character.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Sexton wrote:
Campbell Ritchie wrote:And don't use \n least of all in printf. Use %n for the correct line end. Only use \n if you have been told to produce an LF character.


What is a LF character?


LF is an abbreviation for line feed. On Unix and Mac OS X, lines in a text file or console display are normally ended with only a line feed character. On Windows, a carriage return character followed by a line feed character ends a text line.

The printf representation of a linefeed character is \n (and the representation for carriage return character is \r ).

Java's printf also has something called a line separator which is represented by %n . So you should use %n instead of \n to end a line , because then the printf will output the \n on Unix and \r\n on Windows.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote:The more you look, the more complicated it becomes. . . .
And Red Smith gave such a nice simple explanation. Not sure whether that merits a +1 or a cow. Let's try for a cow

You can use %n in Formatter#format, System.out/err.format, (=PrintStream#format), String#format and printf. I believe all those methods call Formatter#format behind the scenes.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Earlier, I wrote:The more you look, the more complicated it becomes. . . .
And Red Smith gave such a nice simple explanation. Not sure whether that merits a +1 or a cow. Let's try for a cow

You can use %n in Formatter#format, System.out/err.format, (=PrintStream#format), String#format and printf. I believe all those methods call Formatter#format behind the scenes.


Thanks! I had never noticed, or maybe paid attention to, those cows on the left.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'we welcome they are a relatively new feature. We spent ages getting over the objections from Bart Simpson.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!