Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   printf Problem (http://www.programmingforums.org/showthread.php?t=12348)

Eric the Red Jan 10th, 2007 8:52 PM

printf Problem
 
:

String str = new String( "Java is object oriented.");
System.out.printf("|%s|%n", str);


The above code will work in "EditPlus" (which is what my college uses). While, it won't work in "Eclipse".

What would be the reason behind this?

Error:
:

The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, String)

Booooze Jan 10th, 2007 10:05 PM

Is this all the code, and are you sure there are no differences? That's the first step.

Then, the only thing I could think of is to check to see if your compiler by passes certain things - check your settings for that, but that's a long shot (I'm Guessing). Look here for the docs page and compare again.

http://java.sun.com/j2se/1.5.0/docs/...ang.Object...)

Sorry I can't help ya more bud.

titaniumdecoy Jan 10th, 2007 10:23 PM

Baffling.

The printf function of PrintStream (of which System.out is an instance) is new to Java 5. Most likely you need to update the version of Java your computer is running.

According to the documentation, your code is incorrect; the second argument to printf should be an Object array containing the "arguments referenced by the format specifiers of the format string." However, when I run your code in Eclipse on my computer, it works fine.

Booooze Jan 11th, 2007 7:56 PM

Quote:

Originally Posted by titaniumdecoy (Post 122468)
Baffling.

The printf function of PrintStream (of which System.out is an instance) is new to Java 5. Most likely you need to update the version of Java your computer is running.

According to the documentation, your code is incorrect; the second argument to printf should be an Object array containing the "arguments referenced by the format specifiers of the format string." However, when I run your code in Eclipse on my computer, it works fine.

Oops. That's the other thing I meant to mention. :rolleyes:

Eric the Red Jan 11th, 2007 10:19 PM

First off, Thank you for you help.

I've tried installing the newest Java. However, it still doesn't work.

At least now I know that its not eclipse doing this. Any other ideas?

mrynit Jan 11th, 2007 11:32 PM

try running your code thru command line
:

C:\>javac Test.java
C:\>java Test


if javac cannot be found then set your system path to C:\Program Files\Java\jdk1.5.0_02\bin or what version you have.

im guessing Eclipse is still pointing to the older version of java. try checking to see that it is using the new javac.

btw i dont know much about java.

Eric the Red Jan 12th, 2007 1:12 AM

I've ran my code through command line... I placed my 'String1.java' file to the root of the c drive so that.

:

javac C:\String1.java

Actually compiles the code without errors. However, in Eclipse I looked through the settings and found that the 'java build path' seems to be pointing to the right location. So basically, Eclipse still won't compile the code.

WOW.. I'm 100% confused.

mrynit Jan 12th, 2007 12:26 PM

well you know its Eclipse not anything else. double check paths and settings.

try NetBeans?

Arevos Jan 12th, 2007 12:46 PM

IIRC, Eclipse defaults to Java 1.4 compatibility. In other words, even if you have Java 5 on your machine, by default it will compile your code as if it were for 1.4. This is why printf is not working for you.

Here is a page a quick google search pulled up. Assuming Java 5 is the only version of Java installed on your machine, then this step is the only important one:
Quote:

- Set the Eclipse>Preferences>Java>Compiler level to 5.0
If you set that option, your code should compile.

titaniumdecoy Jan 12th, 2007 4:20 PM

As I mentioned previously, according to Sun's documentation, your code is incorrect. As such, it should not be a surprise that it does not compile in some situations.


All times are GMT -5. The time now is 1:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC