0

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);
4
Contributors
7
Replies
10
Views
5 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • As always, it's hard to beat the official Sun/Oracle documentation for Java [url]http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html[/url] Read More

0

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?

0

%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

Edited by zeroliken: n/a

0

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.

0

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.

0

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.

Edited by JamesCherrill: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.