# Java: can you use concatenation with printf?



## Veni_Vidi_Vici (Aug 27, 2007)

The title is pretty self explanatory.

I'm trying to do


```
temp = (celsius /5 *9) +32; 
System.out.printf ("Temperature %.1f", celsius); 
System.out.printf (" degrees Celsius = %.1f", temp); 
System.out.printf (" degrees Fahrenheit\n\n");
```
to only use one line. I just can't seem to figure it out. (well, two lines, i only was to use one "system.out.printf()" statement")

(all variables are double)

Thanks.


----------



## volatile (Aug 14, 2007)

Veni_Vidi_Vici said:


> The title is pretty self explanatory.
> 
> I'm trying to do
> 
> ...


I wasn't familiar with concatenating with this, but after reading a little bit on the API it seems like you can do something along the lines of this:

printf("Temperature %1$f degrees Celsius = %2$f degrees Farenheit\n\n", celsius, temp);

Sorry if I have the format strings wrong. I'm more familiar with C#.


----------



## Chicon (Jul 29, 2004)

Hi Veni_Vidi_Vici,

Welcome to TSG ! 

*System.out* is an object of the type *PrintStream* and there's no *printf* method for that kind of object.

The only valid methods to print are *print* and *println* : both of them allow concatenation.

Example :


```
[SIZE=2]
String user = "Veni_Vidi_Vici";
System.out.println("Hello " + user + " !");
[/SIZE]
```
On this page, you'll find all the description of the *PrintStream* object.


----------



## volatile (Aug 14, 2007)

Chicon said:


> Hi Veni_Vidi_Vici,
> 
> Welcome to TSG !
> 
> ...


You are looking at the 1.4 Java API.

http://java.sun.com/j2se/1.5.0/docs/api/


----------



## Chicon (Jul 29, 2004)

volatile said:


> You are looking at the 1.4 Java API.
> 
> http://java.sun.com/j2se/1.5.0/docs/api/


Hi volatile,

You're right ! Therefore the *printf* method is valid too. 

BTW, I've a few shortcuts to refresh.


----------

