• 0

Help! JAVA printf


Question

First of all I want to say I HATE this class with a passion. My teacher is no help and printf isnt even in the index of my huge book. Anyways. I was given:

public class PrintfDemo

{

	public static void main(String[] args)

	{

		String aString = "abc";



		System.out.println("String output:");

		System.out.println("START1234567890");

		System.out.printf("format", aString);

		System.out.printf("format", aString);

		System.out.printf("format", aString);

		System.out.println( );



		char oneChracter = 'Z';



		System.out.println("Character output:");

		System.out.println("START1234567890");

		System.out.printf("format", oneChracter);

		System.out.printf("format", oneChracter);

		System.out.println( );



		double d = 12345.123456789;



		System.out.println("Floating-point output:");

		System.out.println("START1234567890");

		System.out.printf("format", d);

		System.out.printf("format", d);

		System.out.printf("format", d);

		System.out.printf("format", d);

		System.out.printf("format", d);

		System.out.printf("format", d);

	}

}

And I am suppose to format it using printf to look like:

String output:
START1234567890
STARTabcEND
START abcEND
STARTabc END

Character output:
START1234567890
STARTZEND
START ZEND

Floating-point output:
START1234567890
START12345.123457END
START12345.1235END
START12345.12END
START 12345.1235END
START1.234512e+04END
START 1.23451e+04END

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0
I cant get it to print "Start" then the variable abc then another "END"

Try this:

System.out.printf("START%sEND\n", aString);

You simply tell printf where you want the variable printed using %s in the format string.

For the next part, with chars, you use %c like so:

System.out.printf("START%cEND", oneChracter);

The floats are a little trickier... To specify one decimal place you'd do: %.1f

System.out.printf("START%.1fEND", d);

It's modeled after the C printf() function.

Link to comment
Share on other sites

  • 0

5Horizons explained it in his post.

System.out.printf("START%sEND\n", aString);

You simply tell printf where you want the variable printed using %s in the format string.

printf takes the variable 'aString' and puts it where "%s". Its like a place holder.

Link to comment
Share on other sites

  • 0
How does the system know to put the "aString" in between start and end with:

System.out.printf("START%sEND\n", aString);

just wondering, all this makes little sense.

AND! How do I incorporate spaces, for things like :

START abcEND

rather then just

STARTabcEND

It just looks for occurrences of the percent sign and then uses the character after it to determine how the data type is printed. Then it replaces the 1st %s with the 1st variable you specify, and so on.

Like, say you had printf("START %s %s END\n", aString, aString); you'd get START abc abc END. The \n is a newline character, btw.

Link to comment
Share on other sites

  • 0

You're already better then my instructor lol, and explain better then anything I have found online so far.

I have another question:

START1.234512e+04END Needs to be printed, I have gotten to there withyou instructions, how do I tell the computer to calculate "234512e+04" after the decimal point.

Link to comment
Share on other sites

  • 0

You're amazing! lol

Now I am working on the second ( of 2 ) problems. =)

I cant figure out the logic to separate change from a vending machine:

You can only use 1$ (100) and it figures the change, such that it returns quarters, dimes, nickels when needed.

Its rough..

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.