1

I wish to return a string from a method object that is called by another method in another class. My problem is: when I attempt to assign the returned value to a String in the other class, it cannot find the method object within the class object.

Guessing Game = new Guessing();

This makes the object using the class Guessing.Java

else if (buttonObj == guess){
        double g = yourGuess.getNumber();
            if ((g > 0)&&(g < 11)){
                Game.StartGame(g);
                label3.setVisible(false);
                yourGuess.setEnabled(false);
                label1.setText(Game.StartGame());
            }else{
                label3.setVisible(true);
                yourGuess.requestFocus(true);
            }
    }

When I try retrieving the String from the StartGame method within the Guessing.Java class, it says it cannot find the class.

public String StartGame(double guess){
    int round = 1;
    int guesses = 3;
    String correct = "correct";
    if (guesses > 0){
        if (guess == ans){
            correct = "correct";
        }else if ((guess == ans - 1)||(guess == ans + 1)){
            correct = "hot";
            guesses--;
        }else if ((guess == ans - 2)||(guess == ans - 2)){
            correct = "warm";
            guesses--;
        }else{
            correct = "cold";
            guesses--;
        }
    }else{
        correct = "round";
    }
    return correct;        
}

I have tried several different things and looked it up multiple times but nothing works, can anyone help?

2
  • Its good to share the full exception. Commented Nov 4, 2010 at 3:43
  • check your packages and imports first. Commented Nov 4, 2010 at 3:45

3 Answers 3

1

First of all fix your code by using these Naming Conventions.

Change your code to this,

    if (buttonObj == guess){
        double g = yourGuess.getNumber();
            if ((g > 0)&&(g < 11)){
                String startGameStr = Game.StartGame(g);
                label3.setVisible(false);
                yourGuess.setEnabled(false);
                label1.setText(startGameStr);
            }else{
                label3.setVisible(true);
                yourGuess.requestFocus(true);
            }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

It is hard to tell what is causing the "class not found" exception with the details given.

But one thing I could see it : You are calling the method as:

label1.setText(Game.StartGame());

But the method expects a double argument.

1 Comment

I knew that much, but no matter what I did, I couldn't get it to except it, even with a double argument.
0

according to the first code segment there two overloaded methods in Guessing class

  1. StartGame(Double g)
  2. StartGame()

seems to be like you are calling the second method when you try to assing the returned string to that label which probably retuning emty string or since that method doesn't exist you get method not found exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.