Here's what I do:
double x = 7.0;
System.out.printf("%.2f", x);
Eclipse gives me this error "The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, double)"
Are you using a version of Java older than 1.5? Or maybe an older compiler compliance setting in Eclipse? (e.g. 1.4) In fact, I am pretty sure that is the cause - I just switched my compliance setting to 1.4 and I get the same error as you.
Check your Project's Compiler Compliance setting:
Select the Project
Right click and choose Properties
go to 'Java Compiler'
change your compiler compliance and ensure you are using a JRE of that version or higher
This will work once you are using Java 1.5 or higher, since the printf method was added in 1.5.
I ran the following and I didn't seem to have this problem. Are you getting an error from Eclipse's code inspection, or from the Java compiler?
public class TestDouble {
public static void main(String[] args) {
double x = 7.0;
System.out.printf("%.2f", x);
}
}
This will also work and may stop Eclipse from complaining:
public class TestDouble {
public static void main(String[] args) {
double x = 7.0;
System.out.printf("%.2f", new Double(x));
}
}
Related
I have downloaded eclipse Luna. But the format printing is not working. let consider the simple code given below:
public class Test {
public static void main(String args[]) {
int a=4;
System.out.printf("%d",a);
}
}
It is showing error message below:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method printf(String, Object[]) in the type PrintStream is not applicable for the arguments (String, int)
What may be the possible cause and solution?
Your Compiler compliance level might not be correct.
You can find it in Eclipse here:
Project > Properties > Java Compiler
Make sure it's set to 1.5 or higher.
i have got my answer from the comment of #Katja Christiansen. the comment has given below, as #Katja Christiansen not write the solution in asnswer for that i am wrriting it here let other can find that the problem has solved..
"Maybe project specific compiler settings are enabled? Open the properties of your Java project, select "Java Compiler" and check if "Enable project specific settings" is enabled and if the selected JDK differs from the Eclipse JDK settings. – Katja Christiansen "
after trying it, the printf has solved and it solved for all the existing project.
Thanks #Katja Christiansen for ur cooperation.
I'm using Eclipse Luna and the following main method runs for me:
public class StackOverflow {
public static void main(String[] args) {
int a = 4;
System.out.printf("%d", a);
}
}
Gives me the output of:
4
Try using the format method:
int a = 4;
System.out.format("%d", a);
This is the second time I've tried to use the PrintWriter#printf method, and I get this error message:
The method printf(String, Object[]) in the type PrintStream is not applicable for the argument (String, String)
The code I'm using has two classes.
This is the first class:
class apples4 {
public static void main(String[] args) {
tuna4 tuna4Object = new tuna4("Kelsey");
tuna4Object.saying();
}
}
This is the second class:
public class tuna4 {
private String girlName;
public tuna4(String name) {
girlName=name;
}
public void setName(String name) {
girlName=name;
}
public String getName() {
return girlName;
}
public void saying(){
System.out.printf("Your first girlfriend was %s\n", getName() );
}
}
Check your compliance level...
PrintStream#printf method is available since Java SE 5. Looks like your code is being compiled/evaluated by Java 4 or prior.
Review your JDK installation and/or your IDE settings about how it is compiling/evaluating your code.
By the way, if using Eclipse and Java 8, Eclipse needs a plugin to recognize Java 8 applications, so by default the evaluator will downgrade your project to Java 1.4. This happened to me and I solved it by installing an update in Eclipse Kepler. Eclipse Luna (latest Eclipse version)says that it supports Java 8, but didn't work for me (not sure if I followed the right steps or did something wrong, but went back to Kepler and works fine).
It might sound weird, but you can cast the return value of your getName() method to Object:
System.out.printf("Your first girlfriend was %s\n", (Object) getName());
Or (to create the requested array) even
System.out.printf("Your first girlfriend was %s\n", new Object[] {(Object) getName()} );
could help.
I'm sorry about my previous post saying I had the same problem, I didn't read the "before you post read this" dialog box that says don't do that. Well, after a lot of time looking around for the answer, I figured it out myself. In Preferences->Java->Compiler box, there is a button in the top left corner called "Configure Project Specific Settings...". Click it and either change the compliance level to >= Java 1.5, or turn it off. Boom, fixed.
you might need to change the execution environment if it's not already java SE 1.8
it might be CDC or sth else , so u need to change it to java SE 1.8
details in picture 1.expand ur project then 2.right click on JRE System Library and choose properties finally 3. if the environment is not java SE 1.8 ,change it to become so
I want to use JOMP API (equivalent to OpenMP in C) but I met some problems:
This is the code I want to run:
import jomp.runtime.*;
public class Hello
{
public static void main (String argv[])
{
int myid;
//omp parallel private(myid)
{
myid = OMP.getThreadNum();
System.out.println("Hello from " + myid);
}
}
}
It is just an hello worl but I have a problem with the compiler. Please have a quick look at this page to understand:
http://www2.epcc.ed.ac.uk/computing/research_activities/jomp/download.html
But I can't, I do not understand how it works... I can only compile it with eclipse default compiler (I guess) and then I have only one thread!
I understand I have to compile this code (in a .jomp file) with
java jomp.compiler.Jomp MyFile
and then compile normally but I can't do this in ecplise neither in the terminal (I do not know how to install this compiler!)
ps: I use Ubuntu 12.04 on a Intel® Core™ i7-3610QM CPU # 2.30GHz × 8.
You just need to add the JOMP parameters to your launch configuration, this example can help you:
JOMP eclipse workaround
I have the following code in netbeans (using javafx in the same project):
public class ExperimentControler {
public static HashMap<String,Double> userInput = null;
public static ObservableMapWrapper<String,Double> userInputObservable = null;
}
and
static final String totalDistance = "Total distance";
public static void main(String[] args) {
ExperimentControler.userInput = new HashMap<String,Double>();
ExperimentControler.userInput.put(totalDistance, 300.0);
ExperimentControler.userInputObservable = new ObservableMapWrapper<String,Double>(ExperimentControler.userInput);
Application.launch(PhysicsGui.class, args);
}
#Override
public void start(Stage primaryStage) {
ExperimentControler.userInput.get(totalDistance);
//...
}
This is working perfectly inside netbeans.
If I "clean and build" the project, the resulted .jar file throws a null pointer exception on this line:
ExperimentControler.userInput.get(totalDistance);
Also, this is my java version outside of netbeans:
>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
I also tried with jre 1.7.0 but the results were exactly the same..
In netbeans I have jdk 1.6.0_26.
OK... thanks to Kal's comment:
How are you running this program? Have you tried putting
System.out.printlns() in your main method to make sure that they are
called before the app crashes with a NPE?
I figured out that the following (javafx) code (must be this.. there is no other entry point):
#Override
public void start(Stage primaryStage) {
bypasses the main() when I run it as standalone. Maybe the root cause is totally different I don't know..
The fact is that in netbeans, main() is running and on the standalone is not..
I also checked the jar's manifest and the main-class is correct. (just in case)
My mind could not go to the fact that main is not running at all !
So, I moved the code I had in main() to the overrided start method and it works.
The specification says that the start() method is the main entry point for javafx applications.
But, in my understanding, main() should still be called before start().. this could be a bug on javafx.
I had something like this before.
Hashmap auto-boxing might be the problem here.
I think you are trying to autounbox a null value.
Try
ExperimentControler.userInput.put(totalDistance, new Double(300.0));
\EDIT OK thanks #hovercraft, If this doesn't work, you must be storing a null in your hash map somewhere else in your code. Remember that get(totalDistance) is replaced by get(totalDistance).doubleValue(); if you are assigning to an double.
As to why it doesn't work out of the jar... no idea sorry.
PS What is the exact line for ExperimentControler.userInput.get(totalDistance);? are you assigning it to an Double or a double? that can make all the difference.
Trying to build against javax.vecmath using the 1.5.2 jar file (found on Java.net http://java3d.java.net/binary-builds.html, for example).
Try to make a call on, say Point3d;
public class Foo {
public static void main(String[] args) {
Point3d t = new Point3d(1.0, 1.0, 1.0);
System.out.println(t.getX());
}
}
In 64 bit Windows and Linux (I've only tried Ubuntu 10.04, 64 bit), this compiles and runs.
In OS X (10.6.7) it will not compile:
...: cannot find symbol
symbol : method getX()
location: class javax.vecmath.Point3d
System.out.println (t.getX());
This is using the exact same physical vecmath.jar
If instead I use the source directly, it compiles on OS X, but does not run
Exception in thread "main" java.lang.NoSuchMethodError: javax.vecmath.Point3d.getX()D
If I compile the sources myself on OS X to a jar file, and then use the jar in the example above, again, unable to compile.
Now, the fields being accessed are in javax.vecmath.Tuple3d, which is an abstract class with public fields for x, y and z. So on OS X this will work (actually, it seems to work everywhere).
public class Foo {
public static void main(String[] args) {
Point3d t = new Point3d(1.0, 1.0, 1.0);
System.out.println(t.x);
}
}
The thing is, I'm working on a code base that depends on vecmath.jar, and in which the maintainers are on Windows and wish to keep using the accessor methods, but I'm on OS X.
I'm looking to both:
(1) understand what is going on
(2) figure out how to make these sources portable while depending on the vecmath.jar file.
The "Exception in thread "main" java.lang.NoSuchMethodError: javax.vecmath.Point3d.getX()" indicates that not the 1.5.2 version but the Apple version 1.3 of vecmath.jar is accessed. The *getter" and "setter" methods were introduced in 1.5.
Check if Apple's out-dated Java 3D version 1.3 is installed in System/Library/Java/Extensions/ on your Mac. Remove all Java 3D 1.3 related files including vecmath.jar (jar, jnilib), they are useless.
August, InteractiveMesh