-1

First of all, I am new to this website and coding so please forgive my rookie mistakes, second of all my problem is, I intended to make a quiz via eclipse software using java coding (obviously). But everytime i run it, even when i type in the right answer it reacts like i typed in the wrong answer. Could you please point out my mistake?

import java.util.*;

public class Quiz {

public static void main(String[] args) {
    // TODO Auto-generated method stub


    @SuppressWarnings("resource")
    Scanner in = new Scanner(System.in);
    System.out.println("Hi! Welcome to the Quiz! Please insert your username.");
    String userName = in.nextLine();
    System.out.println("Welcome " + userName + "! Let's start! Your first question is what is the capital of Norway?");
    String answer = in.nextLine();
    if (answer == ("Oslo"))
    System.out.println("Good Job! Your next question is how many states are there in U.S.A.?");

else        
{
    System.out.println("You've failed!");
}
    {

        @SuppressWarnings("unused")
    String answer1 = in.nextLine();
    if (answer1 == ("50"))
    System.out.println("Well done! Your next question is where did the first football world cup happen?");
    else        
    {
        System.out.println("You've failed!");
    }
    String answer2 = in.nextLine();
    if (answer2 == ("Uruguay"))
    System.out.println("Well done! Your next question is");
    else        
    {
        System.out.println("You've failed!");
    }
    }
    }
    }
E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • Also I would like to learn how to create a loop in my coding. For example after giving the wrong answer i want it to restart autumatically from question 1. Thanks in advance – Bora Tosyalı Sep 28 '17 at 19:55
  • I don't think Eclipse has anything to do with your question. It appears that's just the IDE you happen to be using. – Fred Larson Sep 28 '17 at 19:56
  • 1
    Welcome to Stack Overflow, please take the [tour], go through the [help], read [ask] to know how this site works. Also read how to indent your code correctly as is, is near to impossible to understand what's happening. Read the linked /duplicate question to learn what's wrong. To restart, read about `do-while`. – Frakcool Sep 28 '17 at 19:58
  • Just use string.equals(otherString) to compare two strings. – Cedric Sep 28 '17 at 20:00
  • I assume that it is off-topic for stackoverflow, as it is a request for debbuging help. Check this on how to debug in eclipse https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-debug-launch.htm – Sergei Sirik Sep 28 '17 at 20:00

1 Answers1

0

It has to do with comparing strings.

Your situation should be solved when you change your if condition to:

 if (answer.equals("Oslo")) { // Do something } 

When using equals() you compare the contents of what you try to compare. If you use == it actually compares the references of what you try to compare. Thus where the object is stored (answer) to the given string "Oslo", which is not equal and therefore returns false.

In addition to your question in the comments

To get a quick solution you can use nested if-else statements.

You can also use a new if statement for each question, just below each other. This would require a bit more rewriting of your code. See the example:

public class App {

   public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Hi! Welcome to the Quiz! Please insert your username.");
        String userName = in.nextLine();
        System.out.println("Welcome " + userName + "! Let's start! Your first question is what is the capital of Norway?");
        String answer = in.nextLine();
        if (!answer.equals("Oslo")) {
            System.out.println("You've failed!");
        }

        System.out.println("Good Job! Your next question is how many states are there in U.S.A.?");
        answer = in.nextLine();
        if (!answer.equals("50")) {
            System.out.println("You've failed!");
        }
        System.out.println("Well done! Your next question is where did the first football world cup happen?");
    }
}

However, as you can see code becomes pretty repetitive, so making use of function would be a good next step. See the next example:

public class AppWithFunction {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.println("Hi! Welcome to the Quiz! Please insert your username.");
        String userName = in.nextLine();
        System.out.println("Welcome " + userName + "! Let's start!");

        handleQuestion(in, "Your first question is what is the capital of Norway?", "Oslo");
        handleQuestion(in, "Your next question is how many states are there in U.S.A.?", "50");
}

private static void handleQuestion(Scanner in, String questions, String answer) {
    System.out.println(questions);
         if (!in.nextLine().equals(answer)) {
            System.out.println("You've failed!");
        }
    }
}

It might be a bit out of your scope for the moment, but if you would have a bunch of questions, storing these in a map (question = key, answer = value), iterate over them and handle them accordingly would be a good next step.(Maybe a nice self-assigned new exercise later on;)).

Crittje
  • 101
  • 1
  • 3
  • 11
  • if (answer.equals("Oslo")) {// System.out.println("Good Job! Your next question is how many states are there in U.S.A.?"); } else { System.out.println("You've failed!"); } i still couldn't manage to get it right. what did i do wrong? can you please write a working example for me? Thanks in advance @Crittje ! – Bora Tosyalı Sep 28 '17 at 23:13
  • Gave you more details in the answer section :). – Crittje Sep 29 '17 at 15:07