Sunday, March 24, 2019

java - Using a not equal operator in Strings



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?


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...