cj oj
cj oj

Reputation: 11

I'm getting an identifier expected error

I am trying to write a simple quiz game.

Trying to lay out one question and answer session with this code sample:

QuizCard(q, a){   
  String question = "What's the name of the lead actor in the movie 'I Robot'?";
  String answer = "Will Smith";

  q = question;
  a = answer;
 }

QuizCard is a constructor but I am getting an identifier expected error. Could someone make me understand what I am doing wrong here please.

Upvotes: 0

Views: 96

Answers (2)

carloabelli
carloabelli

Reputation: 4349

You are getting the error because you are not specifying the parameter types. Also your constructor isn't doing anything because only the local variables are being changed. Sounds like what you really want is something like this:

public class QuizCard {
    private String question;
    private String answer;

    public QuizCard(String question, String answer) {
        this.question = question;
        this.answer = answer;
    }
}

Upvotes: 5

Raja CSP
Raja CSP

Reputation: 172

QuizCard(String q, String a){
    String question = "What's the name of the lead actor in the movie 'I Robot'?";
    String answer = "Will Smith";
    q = question;
    a = answer;
}

Note: You missed to provide identifier which means the type of parameter that you want to provide in the constructor. The constructor doesn't know whether you are passing a String or int or double.

Upvotes: -1

Related Questions