1

I am trying to make a program that inputs the user's name and age and then asks the user on what info he/she wants to know. Here's the program :

import java.util.Scanner;
public class name_and_age {
    static Scanner key = new Scanner(System.in);
    public static void main(String args[]) {
        String name = "NAME";
        String age = "AGE";
        System.out.println("What is your name?");
                String a = key.nextLine();
                System.out.println("What is your age?");
                int b = key.nextInt();

                while()
                {
                    System.out.println("What do you want to know?");
                    String i = key.nextLine();
                if(i.equals(name))
                {
                    System.out.println("Your name is "+ a);
                }
                if(i.equals(age))                   
                {
                System.out.println("Your age is" + b);

                }
                else
                {
                    System.out.println("Please either enter age or name");
                }







    }

}
}

I am confused on what to put in the while loop because I want something like
while( (i.equals(name)) || (i.equals(age)) ) except that I want to put not equals in the place of equals. What should I do?

4
  • 3
    while(( ! i.equals(name)) || (! i.equals(age))) the ! is a not operator Commented Oct 14, 2018 at 11:25
  • Possible duplicate of Java Does Not Equal (!=) Not Working? Commented Oct 14, 2018 at 11:27
  • Possible duplicate of How do I compare strings in Java? Commented Oct 14, 2018 at 12:41
  • You should format your code properly when posting it to StackOverflow Commented Oct 14, 2018 at 18:18

1 Answer 1

0

To put not-equals, you use:

(!string1.equals(string2))

Note the ! in front. That turns whatever follows into the negation of whatever follows. ! true will be false and ! false will be true.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.