1

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!

5
  • Isn't this error clear enough to understand? You just assigned an int to a Question type. Commented Jul 20, 2018 at 1:07
  • Sweet! I can get a 66% by just answering "No alternative"! Commented Jul 20, 2018 at 1:10
  • @Elliott Frisch That is the next part of my investigation. How to get rid of that part. But to make it work, I had to put it for now. Thanks for the heads up.. Commented Jul 20, 2018 at 1:12
  • @Jai Ofcourse I understand the error, I just do not know how to get rid of the error since my input will always be a String value, where the solutions I found on this webpage always get me into this error. Commented Jul 20, 2018 at 1:13
  • Use a list if it is allowed. Commented Jul 20, 2018 at 1:21

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your correction! I tried it, and it seems to work, though I now need to find out how to initialize the value. Learning to code is getting from one problem into the other!
1

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.

1 Comment

Thanks for the help! Appearantly I was close than, since I first changed it to String, but it didn't work.. Now let me find how to initialize this value..

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.