简体   繁体   中英

Syntax error on token “;”, , expected when initializing called value from another class

I have a value that I want to pass between 2 classes, but I get the following error:

Syntax error on token ";", , expected when initializing called value from another class

Class where I have the initial value

public class Game extends Activity implements OnClickListener{

    RandomMathQuestionGenerator question = new RandomMathQuestionGenerator();
    private static final String TAG= "Sudoku";

    public static final String KEY_DIFFICULTY = 
            "org.example.sudoku.difficulty";
    public static final int DIFFICULTY_NOVICE = 1;
    public static final int DIFFICULTY_EASY = 2;
    public static final int DIFFICULTY_MEDIUM = 3;
    public static final int DIFFICULTY_GURU = 4;

    public int value = 0;

    private GameView gameView;  

    public Game()
    {
       if (KEY_DIFFICULTY == String.valueOf(1))
        {
            value = 2; 
        }
        else if (KEY_DIFFICULTY == String.valueOf(2))
        {
            value = 3;        
        }
        else if (KEY_DIFFICULTY == String.valueOf(3))
        {
            value = 4;
        }
        else if (KEY_DIFFICULTY == String.valueOf(4))
        {
            value = 6;
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");    



    }
    @Override
    public void onClick(View v) 
    {           
          //code here       
    }
}

Class where I call the value

public class RandomMathQuestionGenerator {

Game num = new Game();
int number = 0;
number = num.Game(value);
//public int number = 0;
//number = num.Game(number); 


private static final int NUMBER_OF_QUESTIONS = 1;
    private static final int MIN_QUESTION_ELEMENTS = 2;
    private final int MAX_QUESTION_ELEMENTS = number;
    private static final int MIN_QUESTION_ELEMENT_VALUE = 1;
    private static final int MAX_QUESTION_ELEMENT_VALUE = 100;
    private final Random randomGenerator = new Random();    

    //rest of irrelevant code below
    }

The syntax error is irrelevant in the long run.

In Android your must start an Activity with an Intent. (See this Developer's Guide article .) When you want to start Game use:

Intent intent = new Intent(this, Game.class);
startActivity(intent);

Also you declare KEY_DIFFICULTY as final (unchangeable):

public static final String KEY_DIFFICULTY = "org.example.sudoku.difficulty";

So your if-else block will never be true in any of these cases:

if (KEY_DIFFICULTY == String.valueOf(1))

And to compare Strings in Java you must use equals() , == will give inaccurate results. ( How do I compare strings in Java? )


If you want to pass the level of difficulty from one Activity to another use the Intent's extras, a public class variable, or another approach found in: How do I pass data between Activities in Android application?


Addition
You just added more code to your question and you have circular logic.

- When you create RandomMathQuestionGenerator object, you will create a Game object
    - When you create Game object, you will create a RandomMathQuestionGenerator object
        - When you create RandomMathQuestionGenerator object, you will create a Game object
            - When you create Game object, you will create a RandomMathQuestionGenerator object
                - When you create RandomMathQuestionGenerator object, you will create a Game object
                    - When you create Game object, you will create a RandomMathQuestionGenerator object
                        - When you create RandomMathQuestionGenerator object, you will create a Game object
                            - When you create Game object, you will create a RandomMathQuestionGenerator object
                                - ...

This will only stop when your app throws a StackOverflowException.

number = num.Game(value);

I don't see a method Game(int value) in your Game class. You need to create this method in your class:

public int Game(int value){
   //code
}

I am also not sure you can name a method the same name as your class. I assume the fact it has a return value in the signature, it would be valid, but it would be bad practice to have a method name the same as your class anyway.

Case - In <init>

Here's my modified proposed code for RandomMathQuestionGenerator :

public class RandomMathQuestionGenerator {
  private Game num;
  private int number;

  public RandomMathQuestionGenerator() {
    num = new Game();
    number = num.value;
    // Existing code here
  }
  // Existing code here
}

Case - In method code

Game num = new Game();
int number = num.value;

Here's what's wrong:

  • public Game - public is a field modifier, not a variable modifier, and is invalid in the middle of method code. Note that final is a valid modifier - it means that re-assigning the variable is a compile error.
  • Creating number with a placeholder value, then assigning to it - that 0 will never be used
  • num does not have a method Game, and value is not defined in this code. value is a property of Game objects, of which num is one. I strongly recommend renaming this variable - maybe gameInst would work.

Summary

(in progress)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM