0

I am trying to use printf to display something like this:

Temperature: # # # # # # # # # # # #

but when I run my program, I get this:

Temperature:Temperature:Temperature:Temperature:Temperature:Temperature:Temperature:Temperature:Temperature:Temperature:Temperature:Temperature:
  69.0      67.0      66.0      64.0      66.0      69.0      67.0      67.0      70.0      69.0      69.0      70.0

Here is a snippet of my code:

System.out.printf("Temperature:", "%10.1f", temperature);

Any help will be greatly appreciated.

1
  • System.out.printf("Temperature: %10.1f", temperature); ? Commented Nov 13, 2013 at 17:51

4 Answers 4

1

It should be something like

System.out.printf("Temperature %10.1f\n", temperature)
Sign up to request clarification or add additional context in comments.

1 Comment

this does not give the expected Temperature: # # # # # # # # # # # # format
0

Not sure that you are using a loop or not.

If you are using for loop then write System.out.print("Temperature:"); outside the loop.
then inside the loop you can write System.out.print(temperature+"\t");

Comments

0

There are a number of ways to do this depending on what you want.

If you are using a Collection to store the temperatures, you can iterate through them and print each temp with a space or tab between them.

System.out.print("Temperature: ");
for(int temp: temps)
{
    System.out.print(temp + " ");
}

If you don't care too much about the format of the output you could do the following:

System.out.printf("Temperature: %s", Arrays.toString(temperature.toArray()));

That will give you this:

Temperature: [10.0, 15.0, 20.0]

The last answer will of course only work in situations where your objects have a toString() implementation.

Comments

0

Why not do something like:

String temp = "";

for (strintg s : Temperature){
     temp+= s + " "; }

System.out.printf("Temperature: " + temp);

Pass all the info from the temperature array into a string ahead of time and then print it out as one string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.