Member Avatar for theadjectivenou

I need help trying to figure out what I am doing wrong. my teachers wants two different sentences to read as "Wow, (insert word 1) and (Insert word 2) are funny" and on the second line he wants them reversed to read "Wow, (insert Word 2) and (Insert Word 1) are funny!" this is my code.

public class WoW
	public static void main( String[] args )
	System.out.printf ( "%s\n%s\n" ,
		"Wow" , "red and read are funny words!" + "%s\n%s\n" , 
		"Wow" , "read and red are funny words!" );

and This is my error report

red and read are funny words!%s

I have zero clue what I did wrong.

Member Avatar for theadjectivenou

I tried various ways I added in () + () between the 2 sentences and I still get an error. I removed the "%s\n%s\n" and get
red and read are funny words!Wow

I can't figure out what I did wrong sense it seems I need the "%s\n%s\n" , in there with out getting an error.

Well, let us first sort out the Printf format.

This is how it is supposed to be:


So in the first code, the only string that will be printed is "%s\n%s\n"
and in that the first %s is replaced by "Wow" and the second %s is replaced by concatenation of "red and read are funny words!" and "%s\n%s\n". That is all! All subsequent arguments are ignored. and hence the last part ("Wow" , "read and red are funny words!") is not taken into consideration at all.

I recommend u use the feature of printf that allows u to specify which argument to print. i.e, %1$s prints the first argument and %2$s prints the second argument. So that way u can pass only "red" and "read" as arguments to printf and use 1$ and 2$ to print them according to your requirements.

BTW, for what purpose was this HW given? To test what skill of urs? Is this to test ur understanding of printf method??

Member Avatar for theadjectivenou

He gave it as a teaser to see how far we understand, so he can have a general understanding to what to stick in our finals, in December.

As Stevanity said, the syntax of printf is

printf(String format, Object... args)

// for example, with 1 argument
System.out.printf("My print out string %s", "one");
// result is to display "My print ouf string one" on the console

// with 2 arguments
System.out.printf("And here is %s with another %s"), "one more", "string")
// result is to display "And here is one more with another string" on the console

As you see above, the only portion string you use as argument is the portion that can be changed. Any other parts that are not changed, you do not put in the argument. This is the way to format how you display.

Another way to think of this as you have color blocks laying out in front of you as...
R G G B ? B R ? G B B
Now you can replace the ? with any color block you want to create a new different arrangement.

i.e. You have color block B and G, you could create

See? The same goes to this printf(). What you need to do is to figure which part can be changeable and which part would stay the same. Then use that as part of the argument.

PS: Remember that the number of arguments must match the number of format you want to replace in the string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.