Marco
Marco

Reputation: 33

Random String array return

I tried to create a code that outputs a random question. However, constantly I get the error 'Incompatible types: Int cannot be converted to Question'. I understand the error, but I could not get the solution to it for this case. I am new to this, so it might be I just couldn't translate the given answers to my own example. The error is in the lines:

        questions[i]=temp;
        temp = questions[index];

This snippet can be found in the following code:

    public static void main(String[] args){
    Scanner keyboardInput = new Scanner(System.in);

    System.out.println("Welcome to the geography quiz");
    System.out.println("Press a key to begin");            
    String start = keyboardInput.nextLine();

    String q1 = "What is the capital of Belgium?";
    String q2 = "\nWhat is the capital of Chile?";
    String q3 = "\nWhat is the capital of the Netherlands?";

    Question[]questions={
        new Question(q1, "Brussels", "No alternative"),
        new Question(q2, "Santiago de Chile", "Santiago"),
        new Question(q3, "Amsterdam", "No alternative")
    };

    takeTest(questions);
}

public static void takeTest(Question[]questions){
    int score = 0;
    int index, temp;
    Scanner keyboardInput = new Scanner(System.in);

    Random rnd = new Random();
    for(int i= questions.length -1; i>0; i--){
        index = rnd.nextInt(i+1);
        questions[index] = questions[i];
        questions[i]=temp;
        temp = questions[index];

        System.out.println(questions[i].prompt);
        String a = keyboardInput.nextLine();          

        if (a.equalsIgnoreCase(questions[i].answer)) {
            score++;
        }else if(a.equalsIgnoreCase(questions[i].alternative)){
            score++;
        }
    } 
    System.out.println("You got " + score + " out of " + questions.length);
}

Thanks for your help!

Upvotes: 1

Views: 46

Answers (2)

Jswq
Jswq

Reputation: 776

Is that you want the temp is Question type? Try the code below if it helps.

public static void takeTest(Question[]questions){
    int score = 0;
    int index;
    Question temp;
    Scanner keyboardInput = new Scanner(System.in);

    Random rnd = new Random();
    for(int i= questions.length -1; i>0; i--){
        index = rnd.nextInt(i+1);
        questions[index] = questions[i];
        questions[i]=temp;
        temp = questions[index];

        System.out.println(questions[i].prompt);
        String a = keyboardInput.nextLine();          

        if (a.equalsIgnoreCase(questions[i].answer)) {
            score++;
        }else if(a.equalsIgnoreCase(questions[i].alternative)){
            score++;
        }
    } 
    System.out.println("You got " + score + " out of " + questions.length);
}

Upvotes: 1

Adam P
Adam P

Reputation: 4703

In your code you declare the variable named temp as an int (i.e. int index, temp;). Later, you attempt to assign questions[i] the value of temp. But questions[i] is of type Question while temp is of type int. Since they have different types, you can't assign on to the other. You probably want to make temp have type Question instead of int.

Upvotes: 1

Related Questions