943,999 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3656
  • Java RSS
Oct 16th, 2009
0

printf and three string arguments

Expand Post »
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:

Quote ...
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.

Quote ...
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.
Last edited by majestic0110; Oct 16th, 2009 at 1:24 pm.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 16th, 2009
0
Re: printf and three string arguments
bug?
test:
Java Syntax (Toggle Plain Text)
  1. String format = "String a = %s String b = %s \n";
  2. System.out.printf(format, a, b);
  3. System.out.printf("String a + %s String b + %s \n", a, b);
  4. System.out.printf(new String("String a = %s String b = %s \n"), a, b);
  5. System.out.printf("String a " + "=" + " %s String b " + "=" + " %s \n", a, b);
  6. System.out.printf("String a " + '=' + " %s String b " + '=' + " %s \n", a, b);
compile one test at the same time
post result
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 18th, 2009
0
Re: printf and three string arguments
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!
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 18th, 2009
0
Re: printf and three string arguments
Which one of 2,..,6 lines in my post #2 gives you compilation error at the command line?
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 18th, 2009
0
Re: printf and three string arguments
They all do quuba lol i.e the ones with >= 3 arg's to printf
Last edited by majestic0110; Oct 18th, 2009 at 8:49 pm.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 19th, 2009
0
Re: printf and three string arguments
Compile with additional javac options to obtain more info:
Java Syntax (Toggle Plain Text)
  1. javac -help
  2.  
  3. javac -version
  4.  
  5. javac -g -verbose StringComparisons.java
  6.  
  7. pause
? odd.
check also from command line version of java
if 1.5 - bad
if >1.5 - ok.

http://java.sun.com/docs/books/tutor...berformat.html
Quote ...
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.
Last edited by quuba; Oct 19th, 2009 at 11:50 am.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to resolve an indirect html url?
Next Thread in Java Forum Timeline: Client - Server Application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC