Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Identifier Expected when referencing a method from another method

I’m new to java so please bear with me while I explain my issue.

I’m receiving an "identifier expected" error and having trouble figuring out why. I’m working on a guessing random number game with 3 different methods, randomNumber() for generating the random number and returning it as an int, userAttempt() for prompting and accepting the user guess and returning it as an int, and isGuessCorrect for comparing the user guess to the generated number.

My problem lies in this third method. I want it to call upon the variables returned from randomNumber() and userAttempt() to compare the two (whether they’re equal or if they’re higher/lower).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Currently I’m declaring isGuessCorrect() as:

public static void isGuessCorrect( randomNumber(), userAttempt() ){}

This is where I’m getting my error.

So far I’ve tried calling the return values as they’re initialized in my main method:

public static void isGuessCorrect( correctAnswer, userGuess ){}

I’ve also tried declaring the values inside isGuessCorrect():

public static void isGuessCorrect( randomNumber(), userAttempt() ){
    int correctAnswer1 = randomNumber();
    int userGuess1 = userAttempt();
}

If all that still doesn’t make sense, here’s my code in it’s entirety:

import java.util.Random;
import java.util.Scanner;
    
public class GuessingGame {

    public static void main( String args[] ){
    
      int correctAnswer = randomNumber();
      int userGuess = userAttempt();

        randomNumber();
        userAttempt();
        isGuessCorrect();
    }
    
// randomNumber() is meant to generate a number between 1-100. 
// For testing purposes the number is then printed out.
// randomNumber() returns int generatedAnswer for future use in isGuessCorrect()
    
    public static int randomNumber(){
       
      Random rand = new Random();
        int generatedAnswer = rand.nextInt(100) + 1;
        System.out.println("Correct answer is: " + generatedAnswer); 
        return generatedAnswer;
    }
    
// userAttempt() is meant to prompt the user to enter in their guess 
// For testing purposes the guess is then printed out.
// userAttempt() returns int guessNumber for future use in isGuessCorrect()
    
    public static int userAttempt(){
      
      System.out.println("Please enter in your guess (1-100)");
      Scanner userGuessInput = new Scanner(System.in);
      int guessNumber = userGuessInput.nextInt();
      System.out.println("Your guess is: " + guessNumber);
      return guessNumber;
      
    }
    
    public static void isGuessCorrect( randomNumber(), userAttempt() ){
      
      if(userGuess == correctAnswer){
         
         System.out.println ("Correct! You win! :)");
      }else if(userGuess < correctAnswer){
         
         System.out.println ("Wrong. Try guessing higher next time!");
      }else{
      
         System.out.println ("Wrong. Try guessing lower next time!");
      }
      
    }


}

>Solution :

try doing this instead

public static void isGuessCorrect(){


  correctAnswer= randomNumber();
  userGuess = userAttempt();

  if(userGuess == correctAnswer){
     
     System.out.println ("Correct! You win! :)");
  }else if(userGuess < correctAnswer){
     
     System.out.println ("Wrong. Try guessing higher next time!");
  }else{
  
     System.out.println ("Wrong. Try guessing lower next time!");
  }
  
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading