Can't Find Symbol in my method instance? - java

This a very quick and i feel obvious mistake but i keep getting the CANNOT FIND SYMBOL
symbol : method print(int,int)
this would lead me to believe that i'm not giving the method the right data type parameters, however..
public class Test
{
public static void main(String[] args)
{
TestSrv srvObj = new TestSrv();
srvObj.print(0, 0);
srvObj.print(1, 1);
srvObj.print(2, 10);
}
}
and this method, what it's meant to do aside, i keep getting errors from the above code for all 3 calls to the print method? I am passing it integers on all 3 occasions?
public class TestSrv
{
public void print(int num, int count)
{
for (int i = 0; i <= count; ++i)
{
System.out.print(num + ". " + "*");
}
}
}

Your code should compile. Make sure that you declare both classes in the same package or that you import TestSrv in Test.java.

You almost certainly didn't compile TestSrv after making changes. Using an IDE such as Eclipse or IDEA will take care of much of that detail for you.

while renaming my method and class and such so that it wasn't what i originally named it(as to not confuse anyone) i actually fixed the problem that i had.. that is why this compiled for everyone xD i feel stupid! thanks again

Related

"GridWorld" for ThinkJava Exercise 5.1

Newbie completeing thinkJava book and trying to figure out one of the answers to the exercises. It calls for me to download the "GridWorld" files and complete the following steps:
Write a method named moveBug that takes a bug as a
parameter and invokes move. Test your method by calling it from main.
Modify moveBug so that it invokes canMove and moves the bug only if
it can.
Modify moveBug so that it takes an integer, n, as a parameter, and
moves the bug n times (if it can).
Modify moveBug so that if the bug can’t move, it invokes turn instead.
I am stuck on number 3, I can not figure out how to pass n into the "move() method"
-Please Help I am a newbie
My Code:
import info.gridworld.actor.ActorWorld;
import info.gridworld.actor.Bug;
import info.gridworld.actor.Rock;
public class BugRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
Bug redBug = new Bug();
world.add(redBug);
world.add(new Rock());
world.show();
moveBug(redBug,5);
System.out.println(redBug.getLocation());
}
public static void moveBug(Bug aBug, int n){
if(aBug.canMove() == true){
aBug.move();
} else {
aBug.turn();
}
}
}
You mean you're stuck on number 3:
Modify moveBug so that it takes an integer, n, as a parameter, and moves the bug n times (if it can).
This means write a loop, looping n times, calling move() once per iteration, if canMove() returns true.
BTW: if (canMove() == true) {...} is the long way to say if (canMove()) {...}.
And fix the indentation of the if statement.
Thanks for pointing me #Andreas
My Solution that worked:
public static void moveBug(Bug aBug, int n){
for(int i = 0; i < n; i++){
if(aBug.canMove())
{
aBug.move();
} else {
aBug.turn();
}
}
}

java sound amplifier method

I have an assignment for a class that has to do with implementing arraylists to work with sounds in java. I have tried to write the method but keep getting this error:
java.lang.ArrayIndexOutOfBoundsException:-254 (in java.util.ArrayList)
I am new to programming so I may have missed something quite simple. Anyway, here is my current method:
public void amplify (double amt) {
for(Integer i : myData){
myData.set(i, (int)(myData.get(i) * amt));
}
}
Thank you. Any help would be appreciated.
Try to use this you just create a local variable j and when you want to set the ArrayList use it like a index like below:
public void amplify (double amt) {
int j =0;
for(Integer i : myData){
myData.set(j, (int)(myData.get(i) * amt));
j++;
}
}

".class" error in Java

I am trying to run this program but I cannot, the compiler is sending me a ".class" error.
Can somebody help me with my problem and if it is possible a general tip about ".class" error?
Here is the program:
import java.io.*;
class Bus
{
private int kostos;
private int plithos;
private int typepiv;
Bus(int x,int y,int z)
{
kostos=x;
plithos=y;
typepiv=z;
}
public void KB(int[] x)
{
try{
for(int i=1;i<5;i++)
{
if(typepiv==2)
{
plithos=plithos+plithos/2;
kostos=kostos-kostos/2;
}
if(typepiv==3)
{
plithos=plithos-plithos/5;
kostos=kostos-kostos*25/100;
}
if(typepiv==1)
{
plithos=plithos;
kostos=kostos;
}
x[i]=plithos*kostos;
}
} catch(Exception ex){
ex.printStackTrace();
}
}
}
class testBus
{
public static void main(String args[])
{
String leof[]=new String[4];
int leof1[][]=new int[4][3];
for(int i=1;i<5;i++)
{
System.out.println("dwste onoma leoforiou");
leof[i]=UserInput.getString();
System.out.println("dwste kostos thesis enilika");
leof1[i][1]=UserInput.getInteger();
System.out.println("dwste plithos thesewn");
leof1[i][2]=UserInput.getInteger();
System.out.println("dwste tupos epibath gia enilikes=1,gia
paidia=2,gia suntaksiouxous=3");
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
----->leof2.KB(KostEnoik);
System.out.print("onoleo");
System.out.print(" ");
System.out.print("plithos");
System.out.print(" ");
System.out.print("kost(EURO)");
System.out.print("typepiv");
System.out.print(" ");
System.out.print("apotelesma kostEnoik");
for(int g=1;g<5;g++)
{
System.out.print(leof[g]);
System.out.print(leof1[g][2]);
System.out.print(leof1[g][1]);
System.out.print(leof1[g][3]);
System.out.print(KostEnoik[g]);
}
}
}
the compiler message says :
testBus.java:56:error:cannot find symbol
leof2.KB(KostEnoik);
symbol:bariable leof2
location:class testBus
1 error
Remove the array brackets [] when invoking KB
leof2.KB(KostEnoik);
and remove the preceding enclosing brace }.
Aside: Java naming conventions indicate that variables start with a lowercase letter e.g. kostEnoik. Also consider giving the method KB a meaningful name, e.g. calculateCost
Read Java naming conventions
concern is with your access
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it is defined i.e. outside for loop and scope is upto for loop and that's why the compiler will not be able to find that varialble .
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it's defined (in this particular case, the for loop) and that's not allowed.
method KB takes an int array as argument, but you don't have to add the [] when passing the argument. The correct line is
leof2.KB(KostEnoik);
However, there's something pretty odd with you logic: you're repeatedly (for loop) setting leof2, but only the last iteration of the loop will have any effect. I'm almost certain that that's not what you actually want, but the correct answer to where Bus leof2 should actually be defined depends on the correction of that issue.
leof2.KB(KostEnoik); this is the main culprit. whether you have imported UserInput.
Also try to go through the Java Basics
any method can be invoked using object when it is non static or class name when it is static. Please consider this link
Get leof2 object out side the For Loop.
Don't type [] when you pass the array as argument "leof2.KB(KostEnoik[]);".

Why does Jcreator show iIlegal start of expression?

I am new at programming and currently in our classes we are learning java. I am trying to create a routine in which I need to use String variables only. Below it is the code in which I am working with:
public static void main(String[] args) throws java.io.IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System. in ));
PrintStream out = System.out;
String hair.equals("damagedHair");
cutHair(marvin);
cleanHair(michelle);
for (int i = 0; i < 2; i++) {
static void cutHair(String marvin) {
String cabello;
marvin.equals(hair);
if (marvin.equals("damagedHair")) {
cabello.equals("newHaircut");
result(hair);
}
static void cleanHair(String michelle) {
String hair;
michelle.equals(hair);
if (michelle.equals(newHaircut)) {
hair.equals("putShampooAndConditioner");
result(hair);
}
static void result(String pHair) {;
PrintStream out = System.out;
out.println("=============");
out.println(pHair);
out.println("=============");
}
}
Jcreator is giving me an error that says Illegal start of expression and also java 50 error ';' expected.
I am not sure why is this coming up and I am a little confused as to whether I am doing something I am not supposed to and how to correct it. Sorry about the double posting, this is the right message. Need some help from you guys to figure this out.
Thanks in advanced!
This is in your main:
for(int i=0; i<2; i++)
{
static void cutHair(String marvin)
{
String cabello;
marvin.equals(hair);
if(marvin.equals("damagedHair"))
{
cabello.equals("newHaircut");
result(hair);
}
}
You cannot define methods inside of main. Also, hair is not in scope here, ie it's in your main, not your method. Additionally, you're constantly only declaring variables, and then using them without them ever having been initialized. For example, in the above method, you have:
cabello.equals("newHairCut")
but cabello was never initialized, this should give you a might not have been initialized warning. Or earlier in your code, you have:
String hair.equals("damagedHair");
Again, this doesn't make any sense. You just declared hair here, you cannot call methods on it until you initialize it. I suggest that you review some tutorials.

Calling a class in main

Hi I seem to have a problem calling a class in a main. Can somebody point it out?
KilometerTabel.java
package pratikum31d;
public static double mijlToKilometer() {
double mijl;
mijl = 0;
for (int i = 1; i < 11; i++) {
mijl = i;
}
double kilometer = 1.609 * mijl;
System.out.println(kilometer + " kilometer" + " dat is " + mijl + " mijl");
return kilometer;
}
Main.java
package pratikum31d;
public class Main {
public static void main(String[] args) {
kilometer = mijlToKilometer();
}
}
You never defined a variable called mijl in main. What value do you expect to get passed to mijlToKilometer?
===UPDATE ===
Your new code will have the following problems:
mijlToKilometer is still declared to expect an argument, so you won't be able to call it with no arguments. You must remove the double mijl from the definition of mijlToKilometer.
Your for loop doesn't do what you think it does, though I'm having a hard time identifying what it's supposed to do.
You must declare mijlToKilometer as public.
public static double mijlToKilometer(double mijl)
What are the packages for KilometerTabel and the main class? You haven't put any public/private/protected modifier before your static method. so by default, it will have default visibility. Which is visible within the package. make sure that you bave both classes in same package OR put a public keyword before methods.
secondly, can you please post exact exception?

Categories