user2948668
user2948668

Reputation: 31

How to only print else and not the if?

So I just downloaded Eclipse, and I am currently in a Computer Programming class in 9th grade, so I don't know a whole lot. I have coded before that class so I am a bit ahead. I am sick today and found Eclipse, I started using it and tested it out. Which is also why you will see the Hello World statement in there. I got carried away and started making more code. I wanted to add some if statements in to my code but I am not sure how to use them correctly yet. In the line of

    System.out.println("So your name is... " + name + ". Right?");
    yes = scan.nextLine();

    if ("Yes" != null)
    {
        System.out.println("Great!");

     if ("No" != null)
        {
            System.out.println("Oh. Please retype it.");
            no = scan.nextLine();
        }
    }

I am not sure why , but the output is, Hello World! Please enter your name --> Justin Please enter your age --> 15 Please enter the year you were born --> 1999 So your name is... Justin. Right? No Great! Oh. Please retype it.

As you can see I typed in no, but it still gave me the return as if I typed Yes also. How can I fix this? Thanks in advance!

import java.util.Scanner;
public class helloworld_main {
private static Scanner scan;

public static void main(String args[])
{
    System.out.println("Hello World!");


    scan = new Scanner(System.in);
    String name, age, year, yes, no;

    System.out.println("Please enter your name --> ");
    name = scan.nextLine();

    System.out.println("Please enter your age --> ");
    age = scan.nextLine();

    System.out.println("Please enter the year you were born --> ");
    year = scan.nextLine();

    System.out.println("So your name is... " + name + ". Right?");
    yes = scan.nextLine();

    if ("Yes" != null)
    {
        System.out.println("Great!");

     if ("No" != null)
        {
            System.out.println("Oh. Please retype it.");
            no = scan.nextLine();
        }
    }

    System.out.println("The age you entered is..." + age + ". Right?");
    System.out.println("The year you were born is... " + year + ". Right?");
    scan.close();

}

}

Upvotes: 1

Views: 87

Answers (3)

Chetan Kinger
Chetan Kinger

Reputation: 15212

The problem is with the following code :

if ("Yes" != null)
    {
        System.out.println("Great!");

     if ("No" != null)
        {
            System.out.println("Oh. Please retype it.");
            no = scan.nextLine();
        }
    }

Try this instead :

if ("Yes".equals(yes)) {
        System.out.println("Great!");
} else {
        System.out.println("Oh. Please retype it.");
        no = scan.nextLine();
}

While this will fix your problem, it won't achieve what you are aiming at. Once the user enters a wrong name, you want him to enter the name again and ask him if that's his right name again. You wan't to continue doing this until the user types "Yes". There's a clue for you. Use a do-while loop.

Upvotes: 3

Jean-François Savard
Jean-François Savard

Reputation: 21004

You are not referring to the yes variable when validating "Yes".

System.out.println("So your name is... " + name + ". Right?");
yes = scan.nextLine();

if ("Yes" != null)
{
    System.out.println("Great!");

 if ("No" != null)
    {
        System.out.println("Oh. Please retype it.");
        no = scan.nextLine();
    }
}

You are actually creating new String object with value Yes and No and check if they are null, they will never be.

What you want to do is

System.out.println("So your name is... " + name + ". Right?"); yes = scan.nextLine();

if (yes != null)
{
    System.out.println("Great!");

 if (no != null)
    {
        System.out.println("Oh. Please retype it.");
        no = scan.nextLine();
    }
}

Your program is illogical, I think what you really want is

Scanner sc = new Scanner(System.out);
String result = sc .nextLine();

do
{
    if("Yes".equals(result))
    {
        System.out.println("Great!");
    }
    else if ("No".equals(result))
    {
        System.out.println("Oh. Please retype it.");
        result = sc.nextLine();
    }
} while(!"Yes".equals(result));

Upvotes: 0

RafaelCaballero
RafaelCaballero

Reputation: 1613

The condition ("Yes" != null) is always true. Note that "Yes" is a String, which is obviously not null.

If you want to check the value of variable yes then try

if (yes.equals("Yes")) ...

Upvotes: 0

Related Questions