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?

Tristo
  • 2,328
  • 4
  • 17
  • 28
  • 3
    `while(( ! i.equals(name)) || (! i.equals(age)))` the `!` is a [not](https://www.tutorialspoint.com/java/java_basic_operators.htm) operator – c0der Oct 14 '18 at 11:25
  • Possible duplicate of [Java Does Not Equal (!=) Not Working?](https://stackoverflow.com/questions/8484668/java-does-not-equal-not-working) – Intsab Haider Oct 14 '18 at 11:27
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Federico Grandi Oct 14 '18 at 12:41
  • You should format your code properly when posting it to StackOverflow – MC Emperor Oct 14 '18 at 18:18

1 Answers1

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.

Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33