Hi all, I hope you are well. I am trying to compile a java file using terminal in Ubuntu, but I am getting the following error message:

1. ERROR in StringComparisons.java (at line 20)
System.out.printf("String a = %s String b = %s \n", a,b);

As you can see, I am passing three String arguments to the printf method, which, as far as I can tell from the API is perfectly legal as this would correspond to the object varargs printf method.

printf

public PrintStream printf(String format,
Object... args)

A convenience method to write a formatted string to this output stream using the specified format string and arguments.

Interestingly, this compiles & executes fine in NetBeans (on the same machine, so same JDK version). Please can someone tell me if I have missed something here? Many thanks.

bug?
test:

String format = "String a = %s String b = %s \n";
        System.out.printf(format, a, b);
        System.out.printf("String a + %s String b + %s \n", a, b);
        System.out.printf(new String("String a = %s String b = %s \n"), a, b);
        System.out.printf("String a " + "=" + " %s String b " + "=" + " %s \n", a, b);
        System.out.printf("String a " + '=' + " %s String b " + '=' + " %s \n", a, b);

compile one test at the same time
post result

Hi quuba, thanks for the reply. Having tried that, I still get the same compilation error at the command line, although it works fine in NetBeans. Seems very odd!

Which one of 2,..,6 lines in my post #2 gives you compilation error at the command line?

They all do quuba lol i.e the ones with >= 3 arg's to printf

Compile with additional javac options to obtain more info:

javac -help

javac -version

javac -g -verbose StringComparisons.java

pause

? odd.
check also from command line version of java
if 1.5 - bad
if >1.5 - ok.

http://java.sun.com/docs/books/tutorial/java/data/numberformat.html

Here is a basic example:
int i = 461012;
System.out.format("The value of i is: %d%n", i);
The %d specifies that the single variable is a decimal integer. The %n is a platform-independent newline character.

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.