The printf method won't work - java

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

Related

Automatic repeated java text entry using Eclipse

I am using Eclipse with Java. I need to define several very similar classes. It gets tedious typing the same thing automatically each time and wondering whether I could set up a short cut. I read Eclipse key bindings but it looks like something must already be in a plugin. This is what I need to type each time
public class SomeClass extends Token {
WebDriver driver = null;
WindowStack stack = null;
#Override
public void init() throw InitException {
super.init();
driver = TestCont.getWebDriver(); // defined and set elsewhere
stack = TestCont.getWindowStack();
}
#Override
public void exec throws ExecException {
}
}
SomeClass is actually some unique name.
I guess I could just keep the text in a file and copy/paste, but it would be nice to create a short cut. I recently saw an online class where someone was using an IDE (I don't know which one it was). He typed psvm and it automatically changed to
public static void main(String[] argc) {
}
and doing something like new SomeClass(parm1, parm2, parm3).var automatically set to
SomeClass var = new SomeClass(parm1, parm2, parm3);
and similarly anything with ".var" at the end would make such a variable. So I am wondering whether there is a way to do something similar (as above) in Eclipse with Java.
Not sure whether it matters but I have
Eclipse IDE for Enterprise Java Developers.
Version: 2018-12 (4.10.0)
Build id: 20181214-0600
OS: Windows 10, v.10.0, x86_64 / win32
Java version: 1.8.0_144
You can define templates in Preferences -> Java -> Editor -> Templates
The content assist takes these into account for template completion (the name of the template).
For example, two of the predefined templates are called sysout and syserr. If you type sys, then trigger code completion, it suggests these two templates. Selecting sysout results in this code being inserted:
System.out.println();
(the template also defines places where other stuff needs to be inserted, where the cursor goes etc. but for your problem that seems like nice-to-have).

Intellij Idea - can't run a simple java class even though the main method is declared

I'm a self-taught coder who is new to both Java & Intellij Idea. I wanted to run a simple Java class called payroll in Intellij Idea Ultimate 2017.3
public class payroll {
public static void main(String[] args) {
int hours = 50;
double grossPay, payRate = 25.0;
grossPay = hours * payRate;
System.out.println("your gross pay is $" + grossPay);}
}
Unfortunately IDEA detects some errors indicating that it can't resolve symbols "String" and "System". And when I tried Run/Edit Configuration, the IDE stated that the main method isn't found in the class, but obviously it is declared already
I've used Eclipse & Netbeans before and things just went smoothly then. Can anyone please point out the main cause of this nuisance for me and how to work it out?
If core Java lang (java.lang.*) classes cannot be resolved you may need to define the JDK you want to use with IDEA. Here is a link to how to define the JDK (Java Development Kit) you want to use for your project(s): LINK.
You may also need to confirm the JDK path is correct by navigating to the folder and ensuring the JDK version you are using is indeed located there.
Hope this helps!
According to the screenshot you have the payroll.java file in src directory.
Try moving that file under src/main/java
The class name you should name with Camel-case. That is first.
Second. You need to import package for System

eclipse luna format printing

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);

Java Vuser on LoadRunner 11.52 not working

Recently I have started testing of some application which are Java based. Now the problem is even if I put a simple statement lik;
lr.output_message("Hello");
nothihng is coming up. I havw already set the classpath and jdk path, no error is coming while compilation but during run time no display is coming up.
Sample Script
import lrapi.lr;
import lrapi.web;
public class Actions
{
public int init() throws Throwable
{
return 0;
}//end of init
public int action() throws Throwable
{
lr.start_transaction("trans1");
System.out.println("Lin 1");
lr.output_message("Error");
lr.message("Pulkit");
lr.end_transaction("trans1",lr.AUTO);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
......................
Actually by doing some R&D the same code is working on 9.52 but not on 11.52 so is it a bug?? can anyone suggest me?
I have another hypothesis. Since you have switched versions the environment is not yet set correctly for 11.52, but it is for 9.52. This could be due to many things, such as installing a 64 bit edition of Java on your 11.52 box and a 32 bit edition on your 9.52 box and then expecting the same results. Perhaps even installing an unsupported version of Java under 11.52, but a supported version under 9.52. Start scoping the differences. Document them well for you will have to take the same setup to your 11.52 load generators for reproduction there.

Eclipse, change compiler command (for JOMP)

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

Categories

Resources