Cannot find symbol error while creating objec of class in main - java

This is my code.
I get an error saying:
variable ana of type Analyse
cannot find symbol: ana.lesUlovligeOrd
cannot find symbol: ana.sensurerTekst
Why?
import easyIO.*;
import java.util.*;
public class Eksamen {
public static void main (String[] args) {
Analyse ana = new Analyse();
ana.lesUlovligeOrd("sensurord.txt");
ana.sensurerTekst("roman.txt", "sensurert-roman.txt");
}
}
class Analyse {
In tast = new In();
Out skjerm = new Out();
void lesUloveligeOrd(String s){
}
void sensurerTekst(String s, String t){
}
}

you called ana.lesUlovligeOrd("sensurord.txt");
but actually void lesUloveligeOrd(String s) in Analyse class.
so you call like this ana.lesUloveligeOrd("sensurord.txt");

Related

Why is a class not being reckognized even though it is in the same package of the one I'm running?

I have two classes in the same "test" directory.
package test;
public class Human {
String name;
public Human(String name) {
this.name = name;
}
public void speak() {
System.out.println(name + ": hello!");
}
}
and
package test;
//import test.Human <- have tried this
public class Hello {
public static void main(String[] args) {
Human bia = new Human("bia");
//test.Human bia = new test.Human("bia"); <- have tried this
bia.speak();
}
}
When I try to run it with java Hello.java, it gives me
Hello.java:4: error: cannot find symbol
Human bia = new Human("bia");
^ symbol: class Human
location: class Hello
I also tried to run all the other versions with java test.Hello.java but it also doesnt work, giving me
Error: Could not find or load main class test.Hello.java
Caused by: java.lang.ClassNotFoundException: test.Hello.java
Picture of my IDE
Also, I just found that it compiles and runs on Eclipse, but not on VS Code.

illegal start of expression: Java Method

Does anyone know why this method will not let me call it. I'm trying to make sure I can call a method with no parameters before I start writing my code. Is there some package or something I need or can someone explain what's going on. Thanks in advance. I'm getting an java: illegal start of expression and red line under () next to validate.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
The method validate may not be defined inside the main method.
Instead, do it like this:
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
You have your curly brace order mixed up; and in doing so, have declared a new method within the main method.
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javamethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
public static void validate() {
System.out.print("Hi World");
}
}
}
You should declare your methods at the class level.
Change your code to this:
import java.util.Scanner;
import java.lang.Math;
/// Start Program
public class javaMethods {
public static void main(String[] args) {
// Create Scanner object
Scanner input = new Scanner(System.in);
validate();
}
public static void validate() {
System.out.print("Hi World");
}
}

Use Soot to analyze a Java program

I am trying to use Soot to perform data flow analysis on a java file which is called Example.java.
Here is my Example.java file, my goal is to know which saySomething method animal.saySomething() will call. Here is the code for Example.java I am using:
package a1;
public class Example {
static Animal neverCalled() {
return new Fish();
}
static Animal selectAnimal() {
return new Cat();
}
public static void main(String[] args) {
Animal animal = selectAnimal();
animal.saySomething();
}
}
abstract class Animal {
public abstract void saySomething();
}
class Cat extends Animal {
public void saySomething() {
System.out.println("purr");
}
}
class Dog extends Animal {
public void saySomething() {
System.out.println("woof");
}
}
class Fish extends Animal {
public void saySomething() {
System.out.println("...");
}
}
class Car { // not an Animal
public void saySomething() {
System.out.println("honk!");
}
}
and here is the code I am using to analyze Example.java using Soot, this code is located in the file: TestSootCallGraph.java which follows here:
package a1;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import soot.*;
import soot.jimple.Stmt;
import soot.jimple.spark.SparkTransformer;
import soot.jimple.toolkits.callgraph.CHATransformer;
import soot.jimple.toolkits.callgraph.CallGraph;
import soot.jimple.toolkits.callgraph.Targets;
import soot.options.Options;
public class TestSootCallGraph extends SceneTransformer {
static LinkedList<String> excludeList;
public static void main(String[] args) {
String mainclass = "Example";
// //set classpath
String javapath = System.getProperty("java.class.path");
String jredir = System.getProperty("java.home")+"/lib/rt.jar";
String path = javapath+File.pathSeparator+jredir;
Scene.v().setSootClassPath(path);
//add an intra-procedural analysis phase to Soot
TestSootCallGraph analysis = new TestSootCallGraph();
PackManager.v().getPack("wjtp").add(new Transform("wjtp.TestSootCallGraph", analysis));
excludeJDKLibrary();
//whole program analysis
Options.v().set_whole_program(true);
//load and set main class
Options.v().set_app(true);
SootClass appclass = Scene.v().loadClassAndSupport(mainclass);
System.out.println(appclass);
Scene.v().setMainClass(appclass);
Scene.v().loadNecessaryClasses();
//enable call graph
//enableCHACallGraph();
//enableSparkCallGraph();
//start working
PackManager.v().runPacks();
}
private static void excludeJDKLibrary()
{
//exclude jdk classes
Options.v().set_exclude(excludeList());
//this option must be disabled for a sound call graph
Options.v().set_no_bodies_for_excluded(true);
Options.v().set_allow_phantom_refs(true);
}
private static void enableSparkCallGraph() {
//Enable Spark
HashMap<String,String> opt = new HashMap<String,String>();
//opt.put("propagator","worklist");
//opt.put("simple-edges-bidirectional","false");
opt.put("on-fly-cg","true");
//opt.put("set-impl","double");
//opt.put("double-set-old","hybrid");
//opt.put("double-set-new","hybrid");
//opt.put("pre_jimplify", "true");
SparkTransformer.v().transform("",opt);
PhaseOptions.v().setPhaseOption("cg.spark", "enabled:true");
}
private static void enableCHACallGraph() {
CHATransformer.v().transform();
}
private static LinkedList<String> excludeList()
{
if(excludeList==null)
{
excludeList = new LinkedList<String> ();
excludeList.add("java.");
excludeList.add("javax.");
excludeList.add("sun.");
excludeList.add("sunw.");
excludeList.add("com.sun.");
excludeList.add("com.ibm.");
excludeList.add("com.apple.");
excludeList.add("apple.awt.");
}
return excludeList;
}
#Override
protected void internalTransform(String phaseName,
Map options) {
int numOfEdges =0;
CallGraph callGraph = Scene.v().getCallGraph();
for(SootClass sc : Scene.v().getApplicationClasses()){
for(SootMethod m : sc.getMethods()){
Iterator<MethodOrMethodContext> targets = new Targets(
callGraph.edgesOutOf(m));
while (targets.hasNext()) {
numOfEdges++;
SootMethod tgt = (SootMethod) targets.next();
System.out.println(m + " may call " + tgt);
}
}
}
System.err.println("Total Edges:" + numOfEdges);
}
}
I receive the following error when executing TestSootCallGraph.java which aims at analyzing Example.java. How can I fix this?
Example
Exception in thread "main" java.lang.RuntimeException: Main-class has no main method!
at soot.Scene.setMainClass(Scene.java:171)
at a1.TestSootCallGraph.main(TestSootCallGraph.java:47)

Error:(7, 22) java: ')' expected when executing my code

I receive the following error:
Error:(7, 22) java: ')' expected when executing my code:
public class Main {
public static void main(String[] args) {
// write your code here
myName( mName:"Gowtham");
}
public static void myName(String mName) {
System.out.println(mName);
}
}
This is the code I wanted to execute but it tells it has errors, learning it through some online courses.
The error is here
myName( mName:"Gowtham");
Just write it as following:
public class Main {
public static void main(String[] args) {
// write your code here
myName("Gowtham");
}
public static void myName(String mName) {
System.out.println(mName);
}
}

Simple App Won't Compile in Eclipse (with plugin)?

my code, being practically identical to the code given in BlackBerry's tutorial, has a syntax error in Eclipse. i'm sure there is some small but i'm just not seeing, but my coworker could not find it as well. any ideas would be greatly appreciated. thanks!
Code:
pushScreen(new ABCScreen());
Error:
Cannot make a static reference to the
non-static method pushScreen(Screen)
from the type UiApplication
here is the complete source:
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class AwesomeBBCalculator extends UiApplication {
public AwesomeBBCalculator() {
AwesomeBBCalculator app = new AwesomeBBCalculator();
app.enterEventDispatcher();
}
public static void main(String[] args) {
pushScreen(new ABCScreen()); // ERROR LINE
}
}
final class ABCScreen extends MainScreen {
public ABCScreen() {
super();
// add title
LabelField title = new LabelField("Awesome BlackBerry Calculator",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
}
public boolean onClose() {
Dialog.alert("Thanks for using the Awesome BlackBerry Calculator!\nGoodbye.");
System.exit(0);
return true;
}
}
The pushScreen method can only be called within an instance of UiApplication. You are trying to call it from a static main method. That does not work. Do this instead...
public void foo()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new ABCScreen()).foo();
}
public void class1()
{
pushScreen(this);
}
public static void main(String[] args)
{
(new NewScreen()).class1();
}
try making an object for the ABCScreen class and then use it or u may try this also:
UiApplication.getUiApplication().pushScreen(new ABCScreen());

Categories