0

Alright so I've been learning Java for almost three days at this point, so I recognize that this is going to sound stupidly simple to most of you. However, I decided to test out what I've learned so far by making a Quiz program. I'm using a scanner to acquire the inputs (the answers to the quiz questions), and comparing the input to a internal String variable (correct1). I keep getting an error in line 8 that says "Scanner and String are incomparable." Why is this so? My code seems to follow a pretty logical, basic progression. Is there another way that I should go about programming this? If so, can you explain why my current syntax doesn't work (so I can learn for next time)? Thanks!

import java.util.Scanner;

class Quiz {
public static void main(String[]arguements) {
System.out.println("Who was the first president of the United States?");
String correct1 = "George_Washington";
Scanner Answer = new Scanner(System.in);
if (Answer == correct1) {
System.out.println("Correct, great job!");}
else{ System.out.println("Wrong, better luck next time.");

    }

}

}

e4a
  • 17
  • 2
  • 8
  • That is because an object of type `Scanner` is not an object of type `String`. You can not compare apples to oranges. However you can use methods the `Scanner` offers that give you the text entered by a user, for example `Scanner#nextLine` which returns a `String`. Here is the documentation: [Java-Doc of Scanner](https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--) – Zabuzard Jul 30 '17 at 17:12
  • 2 things: 1. you can not compare cars against apples... Scanner and String are totally different classes... 2. you normally dont use == to compare objects....the duplicated question refers to case 2 – ΦXocę 웃 Пepeúpa ツ Jul 30 '17 at 17:12
  • Answer.contains(correct1) – Sergii Jul 30 '17 at 17:13

0 Answers0