-5

I am making a simple quiz in Java, however, I am running into some trouble.

import java.util.Scanner;
public class quiz {
    public static void main( String[] args ) {
        Scanner keyboard = new Scanner( System.in );
        String q1 = "London";
        String a1;
        int q2 = 20;
        int a2;
        String q3 = "Java";
        String a3;
        int score = 0;

        System.out.println( "What is the capital of England? ");
        a1 = keyboard.next();
        if(a1 == q1) {
            score + 1;
            System.out.println( "Correct!");
        }
        else if {
            System.out.println( "WRONG!" );
        }

        System.out.println( "What is 10 + 10?" );
        a2 = keyboard.nextInt();
        if(a2 == q2) {
            score + 1;
            System.out.println( "Correct!" );
        }
        else if {
            System.out.println( "WRONG" );
        }

        System.out.println( "What langauge is kevin learning?" );
        a3 = keyboard.next();
        if(a3 == q3) {
            score + 1;
            System.out.println( "Correct!" );
        }
        else if {
            System.out.println( "WRONG" );
        }

        System.out.println( "Your total marks were" score );
    }
}
user2611495
  • 1
  • 1
  • 3
  • 1
    What is *some trouble*? – Adam Siemion Jul 25 '13 at 10:37
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Vincent van der Weele Jul 25 '13 at 10:38
  • Read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) first. – Manuel Jul 25 '13 at 10:38
  • What is wrong exactly? Maybe you could put that instead of "blah blah blah blah..." – tbodt Jul 25 '13 at 10:38
  • 4
    Your problem is blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah, the recommended solution is to blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. – mpenkov Jul 25 '13 at 10:38
  • 1
    This does not compile, you should read the compiler errors for hints on how to proceed. – Dahaka Jul 25 '13 at 10:39

2 Answers2

2

You have to compare String using their equals method == does not compare the String, but the object's location in memory. So, instead of if(a1 == q1) use if(a1.equals(q1))

EDIT: here is the workig code:

import java.util.Scanner;

public class quiz {
  public static void main( String[] args ) {
        Scanner keyboard = new Scanner( System.in );
        String q1 = "London";
        String a1;
        int q2 = 20;
        int a2;
        String q3 = "Java";
        String a3;
        int score = 0;

        System.out.println( "What is the capital of England? ");
        a1 = keyboard.next();
        if(a1.equals(q1)) {
            score += 1;
            System.out.println( "Correct!");
        }
        else {
            System.out.println( "WRONG!" );
        }

        System.out.println( "What is 10 + 10?" );
        a2 = keyboard.nextInt();
        if(a2 == q2) {
            score += 1;
            System.out.println( "Correct!" );
        }
        else {
            System.out.println( "WRONG" );
        }

        System.out.println( "What langauge is kevin learning?" );
        a3 = keyboard.next();
        if(a3.equals(q3)) {
      score += 1;
            System.out.println( "Correct!" );
        }
    else {
            System.out.println( "WRONG" );
        }

    System.out.println("Your total marks were" + score);
    }
}
Matthias
  • 3,582
  • 2
  • 30
  • 41
1

Incorrect String comparison:

if(a1 == q1)

should be:

if(a1.equals(q1))

same story with:

if(a3 == q3)
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92