开发者

[Java error]Cannot find anything named "number"

I'm making a calculator in Java using Processing as a framework. I'm writing开发者_C百科 a class that stores number input so that it can be retrieved later on.

//store numbers in memory
class memStorage {
  float storedNum1, storedNum2;

  //constructor
  void Memory(float num1, float num2){
    storedNum1 = num1;
    storedNum2 = num2;
  }

  //Store numbers and call them when needed
  //Store the first number
  void mem1(float num1){
    num1 = number;
    println("number 1 has been stored");
  }

  //Store the second number
  void mem2(float num2){
    num2 = number;
    println("number 2 has been stored");
  }

}

void processNumber(char number){
  //Instantiate memory storage class and execute method
  memStorage storedNum1 = new memStorage();
  storedNum1.mem1();
  //print keypressed numbers
  println(storedNum1);
}

When I run the sketch in processing it gives me an error that says: Cannot find anything named "number"

I'm sort of stuck as to what I'm supposed to do to get this to work. Any advice is appreciated.


There's tons of syntax issues here. You don't have just 1 issue, you have a lot. Just fixing the one you asked about is going to lead to about 5 - 10 more.

I'll comment them inline first.

//This should probably be MemStorage. In Java classes start with a capital letter.
//It should also probably be public.
class memStorage {  
  float storedNum1, storedNum2;

  //constructor
  //This isn't a constructor. This is a method. It would be a constructor if it matched the name 
  //of the class AND didn't return a type of "void"
  void Memory(float num1, float num2){
    storedNum1 = num1;
    storedNum2 = num2;
  }

  //Store numbers and call them when needed
  //Store the first number
  void mem1(float num1){
    num1 = number; // The value of number is undeclared. This is the syntax error you ran into.
    // Also note that you didn't store in storedNum1.
    println("number 1 has been stored");
  }

  //Store the second number
  void mem2(float num2){
    num2 = number; // The value of number is undeclared. This is the syntax error you ran into.
    // Also note that you didn't store in storedNum2.
    println("number 2 has been stored");
  }

}

// This method isn't within the body of any class. Methods always belong inside of a class.
// The way you write this method, it looks like it should be the main method of another class
// You are using to hand test the MemStorage class
void processNumber(char number){
  //Instantiate memory storage class and execute method
  memStorage storedNum1 = new memStorage();
  storedNum1.mem1();
  //print keypressed numbers
  println(storedNum1); //This method doesn't exist. You probably mean System.out.println()
  // Furthermore, you didn't override toString(), so it wouldn't print anything meaningful.
}

Here's how I would clean this up and retain what appears to be your intention.

public class MemStorage {
    private float storedNum1;
    private float storedNum2;

    public MemStorage(float num1, float num2){
        this.storedNum1 = num1;
        this.storedNum2 = num2;
    }

    public void setNum1(float num1){
        this.storedNum1 = num1;
        System.out.println("Number 1 has been stored.");
    }

    public void setNum2(float num2){
        this.storedNum2 = num2;
        System.out.println("Number 2 has been stored.");
    }

    public float getNum1(){
        return this.storedNum1;
    }

    public float getNum2(){
        return this.storedNum2;
    }

    // Hand Test
    public static void main(String[] args){
        MemStorage memStorage = new MemStorage(0,0);
        memStorage.setNum1(1.23454f);
        System.out.println(memStorage.getNum1());
    }
}

You really need to go back to basics and start with a beginners tutorial.


First, put the processNumber function inside of memStorage class. You'll probably want to convert the 'number' variable as an instance variable along with storedNum1 and storedNum2. And third, create a main() function inside of the class and then make an instance of memStorage class and call the processNumber() function. And fourth, the constructor MUST be the exact same name as the class name. Change it to 'memStorage'.

Also mem1 and mem2 functions doesnt store anything to the instance variable. Once the function exits, the 'num1' and 'num2' variable will vanish. Replace 'num1' and 'num2' to storedNum1 or storedNum2.

You'll need to study and understand the object oriented paradigm concept.


if you look closer at

 num1 = number;

and

 num2 = number;

You'll see that there is no declaration of number before (unless you haven't posted the related code?)

also I think you wanted to do something like

private number = 0;       
void mem1(float num1){
   number = num1;
   println("number 1 has been stored in private field number");
}

your code copies the value of number into a local variable num1 that will be destroyed on exiting the function: Nothing will be stored.


It seems that number is a local variable and that it can't be seen outside its method block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜