• Post Reply Bookmark Topic Watch Topic
  • New Topic

printf error  RSS feed

 
Damien Sky
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I keep doing this, but I solved it on my own again :S I had been trying for hours to understand it and then when I posted it on here, I understood.

The problem was that I wasn't supposed to use the % in front of f.

I have to make a program to calculate the average temperature and annual precipitation in Jacksonville. I get this error on line 49:

java.util.IllegalFormatPrecisionException;
null (in java.util.Formatter$FormatSpecifier)


I don't get any errors during compilation.

Here is my code (I'm not using the system selection yet):

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damien Sky wrote:The problem was that I wasn't supposed to use the % in front of f.

Good to see you figured it out. And left the post up for the next person who has that problem. Have a cow for that!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you have too many print calls there. Rather thanwrite
You will probably only save a few milliseconds, but if you have a million print calls those milliseconds will add up.
Only use \n if you have been asked for an LF character. Use %n in printf instead. Note I have added a final %n to take you to a new line.
 
Damien Sky
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually ended up doing the whole thing with printf instead of \t (My teacher said that the goal was to arrange them into neat columns and I shouldn't use tabs for that because they are unpredictable)

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still too many print calls. Look at line 30


That could read


It probably takes a microsecond or so to create the Strings to print, and a few milliseconds to get to the terminal to display it. The more you can get into a single call while maintaining legibility, the better.
Do you need the space after %n? That is equivalent to writing %49s.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!