Hi all there,

I was reading a book on Java and came across this piece of code. Now, I am not sure how line 13 works especially, these symbols after the equal signs "passed=%d; failed=%d%n" . How do the symbols affect the output of the variables(passed & failed)? so let's say they are left out, then we won't get any values printed out.

What are they called? Are they operators or something like that? Any link you'd share that has a list of them?

Anyone has a minute or two to explain that to me? Thanks in advance.

int passed = 0;
    int failed = 0;
       for (String className : args) {
           try {
               Class c = Class.forName(className);
               c.getMethod("test").invoke(c.newInstance());
               passed++;
           } catch (Exception ex) {
               System.out.printf("%s failed: %s%n", className, ex);
               failed++;
           }
       }
       System.out.printf("passed=%d; failed=%d%n", passed, failed);

Recommended Answers

All 7 Replies

Correction to the earlier post. The example was NOT from the book I was reading. It was actually from Oracle website. I was lookin' at an example of varags.

Thanks guys for the links. But, I still don't know how this line "passed=%d; failed=%d%n" works. What do the format specifiers(like thines01 said) do?

%d is an integer format specifier.

in the code you posted it refers to the integer values of passed and failed

I don't think we can explain this better from the links posted by thines01 and JamesCherrill

Failed and Passed are both ints. What does the %n mean then? why is it used after failed=%d?

It doesn't mean a new line,does it? New line is \n.

Yes, I'm sure that %n was supposed to be \n.
The %d means digits will go there based on what follows the quoted string separated by commas.
A %s represents a place-holder for a string.

Check out the two links that were sent.

Failed and Passed are both ints. What does the %n mean then? why is it used after failed=%d? It doesn't mean a new line,does it? New line is \n.

If you bothered to read the official documentation at the link I gave you the you would have found this:

[B]Conversion 	Argument Category 	Description [/B]
'n' 	         line separator 	The result is the platform-specific line separator

so %n is a valid format string, which is replaced by \n when the format is processed.

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.