Skip to content

learning-java-2825378-03_07b (Code gives incorrect answer even when I type Jupiter) Help #47

Open

Description

@tnbythewood

import java.util.Scanner;

public class Main {

public static void main(String args[]) {
    String question = "What is the largest planet in the solar system?";
    String choiceOne = "Earth";
    String choiceTwo = "Jupiter";
    String choiceThree = "Mars";

    String correctAnswer;
    correctAnswer = choiceTwo;

    // Write a print statement asking the question
    {System.out.println("What is the largest planet in the solar system?");}
    // Write a print statement giving the answer choices
    {System.out.println("Choose one of the following:" + " " + "Earth," + " " + "Jupiter," + " " + "Mars");}
    // Have the user input an answer
        Scanner scanner = new Scanner(System.in);
    // Retrieve the user's input
    String input = scanner.next();
    // If the user's input matches the correctAnswer...
    if(choiceTwo.equals(input.toLowerCase()))
    // then the user is correct and we want to print out a congrats message to the user.
    {System.out.println("Congrats! You are correct!");}
    // If the user's input does not match the correctAnswer...
    else {System.out.println("Incorrect, the correct answer is" + " " + correctAnswer);}
    // then the user is incorrect and we want to print out a message saying that the user is incorrect as well as what the correct choice was.

}

}

Activity

  1. leonjura commented on May 29, 2021

    @leonjura

    I would shorten the code, but the main issue here is that you used .toLowerCase() on input but choiceTwo points to "Jupiter" not "jupiter".

  2. CHANDRA-CJ commented on Jun 9, 2021

    @CHANDRA-CJ

    import java.util.Scanner;
    class Whileloop {
    public static void main(String arg[]){
    Scanner scanner = new Scanner(System.in);
    String question =" what is cat? ";
    String optionone =" animal ";
    String optiontwo =" bird ";
    String optionthree =" fruit ";

        String correctanswer = optionone;
    
        System.out.println(question);
        System.out.println(" your options are: " + optionone + ", " + optiontwo + " ," + optionthree + " ");
        String input = scanner.next();
        if (correctanswer.equals(input)){
            System.out.println("your Answer is correct");}
        else{
            System.out.println("Answer is not correct" + correctanswer);
        }
    
    
    }
    

    }

    //having same issue. not reading the if statement

  3. CHANDRA-CJ commented on Jun 9, 2021

    @CHANDRA-CJ

    //okay the .equals is case sensitive. problem solved

    import java.util.Scanner;
    class Whileloop {
    public static void main(String arg[]){
    Scanner scanner = new Scanner(System.in);
    String question =" what is cat? ";
    String optionone ="animal";
    String optiontwo ="bird";
    String optionthree ="fruit";

    String correctanswer = optionone;
    
    System.out.println(question);
    System.out.println(" your options are: " + optionone + ", " + optiontwo + " ," + optionthree + " ");
    String input = scanner.next();
    if (correctanswer.equals(input)){
        System.out.println("your Answer is correct");}
    else{
        System.out.println("Answer is not correct" + correctanswer);
    }
    

    }
    }

  4. bastianhilton commented on Nov 21, 2021

    @bastianhilton

    For me i removed the option .toLowerCase() and moved the scanner to right under the main (not sure if the scanner part helped or not)

    import java.util.Scanner;

    public class HelloWorld {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String question = "What is my name?";
    String choiceOne = "Sebastian";
    String choiceTwo = "Ceres";
    String choiceThree = "Ambrose";

        System.out.println(question);
    
        String correctAnswer = choiceOne;
        
        System.out.println("Choose one of the following: " + choiceOne + ", " + choiceTwo + ", " + choiceThree + ".");
    
        String input = scanner.next();
        
        if (correctAnswer.equals(input)){
            System.out.println("Nice! You know me");
        } else {
            System.out.println("Sorry, You don't know me at all. My real name is " + correctAnswer);
        }
    }
    

    }

  5. Rishabh-Ethicalhacker commented on Jul 28, 2023

    @Rishabh-Ethicalhacker

    Here the main problem is you converted your input in lower case and the value stored in String first letter is in upper case so to avoid this type of problem we can use here equalsIgnoreCase() function

    import java.util.Scanner;

    public class Main {

    public static void main(String args[]) {
    String question = "What is the largest planet in the solar system?";
    String choiceOne = "Earth";
    String choiceTwo = "Jupiter";
    String choiceThree = "Mars";

    String correctAnswer;
    correctAnswer = choiceTwo;
    
    // Write a print statement asking the question
    {System.out.println("What is the largest planet in the solar system?");}
    // Write a print statement giving the answer choices
    {System.out.println("Choose one of the following:" + " " + "Earth," + " " + "Jupiter," + " " + "Mars");}
    // Have the user input an answer
        Scanner scanner = new Scanner(System.in);
    // Retrieve the user's input
    String input = scanner.next();
    // If the user's input matches the correctAnswer...
    if(choiceTwo.equalsIgnoreCase(input))
    // then the user is correct and we want to print out a congrats message to the user.
    {System.out.println("Congrats! You are correct!");}
    // If the user's input does not match the correctAnswer...
    else {System.out.println("Incorrect, the correct answer is" + " " + correctAnswer);}
    // then the user is incorrect and we want to print out a message saying that the user is incorrect as well as what the correct choice was.
    

    }
    }

  6. chetan7252 commented on Aug 6, 2023

    @chetan7252

    Here, you need not use toLowerCase() method and instead of using equals() method, use equalsIgnoreCase() which checks if two strings are equal or not by ignoring its lower or upper case.

    import java.util.Scanner;

    public class Main {

    public static void main(String args[]) {
    String question = "What is the largest planet in the solar system?";
    String choiceOne = "Earth";
    String choiceTwo = "Jupiter";
    String choiceThree = "Mars";

    String correctAnswer;
    correctAnswer = choiceTwo;
    
    // Write a print statement asking the question
    {System.out.println("What is the largest planet in the solar system?");}
    // Write a print statement giving the answer choices
    {System.out.println("Choose one of the following:" + " " + "Earth," + " " + "Jupiter," + " " + "Mars");}
    // Have the user input an answer
        Scanner scanner = new Scanner(System.in);
    // Retrieve the user's input
    String input = scanner.next();
    // If the user's input matches the correctAnswer...
    if(choiceTwo.equalsIgnoreCase(input))
    // then the user is correct and we want to print out a congrats message to the user.
    {System.out.println("Congrats! You are correct!");}
    // If the user's input does not match the correctAnswer...
    else {System.out.println("Incorrect, the correct answer is" + " " + correctAnswer);}
    // then the user is incorrect and we want to print out a message saying that the user is incorrect as well as what the correct choice was.
    

    }

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions