aspose file tools*
The moose likes Beginning Java and the fly likes Help with printf() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with printf()" Watch "Help with printf()" New topic
Author

Help with printf()

Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
Hey everybody, hows it going? Im very confused using this printf() method. Im helping a friend with a thing hes doing for his internship and its basically the blind leading the blind. What we've done so far is read lines from a file, which the line from the file looked like this:
Australia
6.5
4.5

austria
5.6
5.2
and so on

What we had to do was read the lines, calculate the percent change between the first and second number associated with each country and use the print f method to give us a print out that looks like this:
Australia : 6.5 : 4.5 : %change
Austria : 5.6 : 5.2 : % change
We have got everything working except I cant figure out out how to get it to print it out like that.
If anybody could give me a few pointers, I would be very appreciative.
Thanks a lot,
Chas
Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
PS,
This is what I have this so far:
[edit]Add code tags. CR[/edit]
[ October 10, 2008: Message edited by: Campbell Ritchie ]
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
printf is a method of the PrintWriter class. Your first step should be to look at the API documentation: Java 6 - Print Writer API Documentation. There are two signatures to consider:

1) printf(String format, Object... args)
2) printf(Locale l, String format, Object... args)

If you don't care about the locale then you just need to use the first one.

There are different formats that can be specified in the first string, but the one you are concerned with is "%f". That says - Hey, format this as a float. Taking it a step farther, I suspect you are interested in limiting the precision. You can do this by doing something like "%.2f" This will set the precision to 2 digits. Here is a revised working example:

[edit]Add code tags. CR[/edit]
[ October 10, 2008: Message edited by: Campbell Ritchie ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The appropriate API to read would be: PrintStream#printf(String format, Object... args), which points you to: the Format String description.

As a brief intro to using the format String: first ignore the values you want to insert, so any constant text should be added to the String:
" : : : % change"
Then you use the parameter markers to define which parameters will be inserted where in your string. The parameter marker is a %n$. So not the String looks like this:
"%1$ : %2$ : %3$ : %4$% change"

Then insert conversion. How do you want to convert the argument into formatted text? For the first argument, it is easy. You want to insert the String value as is. Looking through the table I see either 's' or 'S' as a conversion will do the job:
"%1$s : %2$ : %3$ : %4$% change"

The next three values are doubles, or floating points, which uses the 'f' conversion.
"%1$s : %2$f : %3$f : %4$f% change"

The thing with floats is that you can define their precision, which is how many places after the decimal point there are, important especially for calculated values. Your example uses a single decimal place which is represented as '.1' before the 'f' flag, so:
"%1$s : %2$.1f : %3$.1f : %4$.1f% change"

Finally, we want to double check special cases. We end up having to escape the % sign used for % change, because % has a special meaning. To do that use %%. We may also want to print a new line at the end of the String, which is %n:
"%1$s : %2$.1f : %3$.1f : %4$.1f%% change%n"

To use this format String in printf we would have to make sure our country name and the two numbers get added to the argument list in the proper order:
System.out.printf("%1$s : %2$.1f : %3$.1f : %4$.1f%% change%n", country, first, second, change);


Steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

it would help terribly if when you post your code, please use the "code" tags. surround your java with these"

[ code]
[ /code]

only without the space. you can use the little 'instant UBB Code' buttons below the 'add reply' or 'add post' or whatever. it just makes it easier to read by preserving the spacing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
Hey guys, Thanks for letting me know that Fred. I had no idea but it really is much easier that way. Ill be sure to do that from now on.

Back to my problem;
I have entered the line:
System.out.printf("%1$s : %2$.1f : %3$.1f : %4$.1f%% change%n", country, first, second, change);
and everytime I do it, the string country has the red squiggles under it. When I place the cursor over it, it says "variable country may not have been initialized"
What did I do?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
There is no way for the compiler to know that your first condition will *always* execute before your fourth condition. It analyzes every branch separately and when it analyzed the branch with the printf(/*...*/, country) it correctly sees that country is not initialized in this branch, and country is not initialized in any non-conditional branch before this point so its possible that the program could reach this point any country is still not initialized. So it reports this as an error.

To overcome this you must ensure that all variables are "definitely assigned" before they are used.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
How can I make sure that the variables are definitely assigned. I have tried everything that I can think of and still, I get nothing. I have it now so it is printing out the first value, the second value and the change but no country name appears. It doesnt even give me an error anymore, it just doesnt print out the country name before the numbers. Any advice??
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The first thing to notice is that I used the variable named country, while you use one named countryName. Perhaps one of them needs to be changed.

Second, if that isn't the current state of things in your code, you should post your code again since we have no idea what your code actually looks like now.
Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
Thanks, I have been using the variable name countryName the whole time. Here is what I have:


Everything seems to be ok except countryName in the printf line still has squiggles
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

All you have to do is initialize countryName when you declare it. You can initialize it to any value, typically null or the empty string. This gets rid of the exception because the compiler knows that the String was initialized, but doesn't affect the output because the variable will be re-assigned in the first look.
Chas Martin
Greenhorn

Joined: Oct 07, 2008
Posts: 18
It works!!! Thank you guys, I appreciate the help so much!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with printf()