This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
The line starting the if statement is causing me issues. When I run it it will ask me my gender, I say boy and it says 'you cannot enter'. It doesnt even ask for the age!
import java.util.Scanner;
class Apples {
//////Main Method
public static void main (String[] args) {
if (Apples.getGender() == "boy" && Apples.getAge() > 17) {
System.out.printf("You can enter");
}
else {
System.out.printf("You cannot enter");
}
}
/////Two Methods
public static String getGender() {
Scanner newGender = new Scanner(System.in);
System.out.println("What gender are you?");
String genderMF = newGender.nextLine();
return genderMF;
}
public static int getAge() {
Scanner newAge = new Scanner(System.in);
System.out.println("What age are you?");
int ageMF = newAge.nextInt();
return ageMF;
}
}
You have to compare strings with .equals function in Java, not with ==.
s1.equals(s2);
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
why isnt this working
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner scan = new Scanner(System.in);
System.out.println("enter year: ");
int year = scan.nextInt();
int age = 0;
System.out.println("is your bday complete this year: ans 'Y/N'");
String bday= scan.nextLine();
scan.close();
if (bday=="Y"||bday=="y"){
age = 2021-year;
}else if(bday=="N"||bday=="n"){
age = 2020-year;
}else{
System.out.println("wrong input");
}
System.out.println(age);
}
}
You need to use equals() method to compare strings. In this case it is better to use equalsIgnoreCase(). Update the if and else if as below:
if (bday.equalsIgnoreCase("y")){
age = 2021-year;
}else if(bday.equalsIgnoreCase("n")){
age = 2020-year;
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
I want the code to be if entered "shoot" then it'll print "Nice, you killed the zombie"
and if the user inputs "don't shoot" then it'll print "oh no, the zombie killed you"
this is what I've done so far but it won't print out anything.
public static void main(String[] args) {
System.out.println("ZOMBIE AHEAD!");
Scanner kb = new Scanner(System.in);
String action1 = "shoot";
String action2 = "don't shoot";
String str = kb.nextLine();
if (str == action1) {
System.out.println("Nice, you killed the zombie!");
} else if (str == action2) {
System.out.println("Oh no, the zombie killed you!");
}
}
}
Use the following:
public static void main(String[] args) {
System.out.println("ZOMBIE AHEAD!");
Scanner kb = new Scanner(System.in);
String action1 = "shoot";
String action2 = "don't shoot";
String str = kb.nextLine();
if (str.equals(action1)) {
System.out.println("Nice, you killed the zombie!");
} else if (str.equals(action2)) {
System.out.println("Oh no, the zombie killed you!");
}
}
}
You have to use .equals() to compare Strings.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Could some one please tell me why this calculator isn't working ? It just doesn't provide an awnser.
import java.util.Scanner;
public class Calcu {
public static void main( String[] args )
{
Scanner mati = new Scanner(System.in);
System.out.println("This program adds up or substracts two numbers");
System.out.println("Enter an operator");
String letter = mati.next(); //WAITS FOR THE PHRASE ADD OR SUBSTRACT
System.out.println("Enter your first number");
int userNumberone = mati.nextInt(); // Get's first Number
System.out.println("Enter your second number");
int userNumbertwo = mati.nextInt(); //Get's Following Number
if(letter == "add") {
int result = userNumberone + userNumbertwo;
System.out.println(result);
} else if(letter == "substract") {
int result1 = userNumberone - userNumbertwo; //If statement to add or substract.
System.out.println(result1);
}
}
}
You should use letter.equals("add") instead of letters == "add". This is explained here: How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
When I try to do a dowhile loop in Java with a string it just terminates the program without an error not giving me a chance to enter if I would like to continue. How do I make my program loop if requested using a string?
public static void main(String args[]) {
Scanner user_input = new Scanner (System.in);
Maths math = new Maths();
double firstNum, secondNum;
String calcAgain = null;
do{
System.out.println("Enter First Number: ");
firstNum = user_input.nextDouble();
System.out.println("Enter Second Number: ");
secondNum = user_input.nextDouble();
System.out.println("Please select an operation! (+,-,*,/)");
String operation = user_input.next();
if (operation.equals("+")) {
System.out.println(math.add(firstNum, secondNum));
}
else if (operation.equals("-")) {
System.out.println(math.subtract(firstNum, secondNum));
}
else if (operation.equals("*")) {
System.out.println(math.multiply(firstNum, secondNum));
}
else if (operation.equals("/")) {
System.out.println(math.divide(firstNum, secondNum));
}
else {
System.out.println("Invalid Function!");
}
System.out.println("Would you like to do another calculation? ");
calcAgain = user_input.nextLine();
}while(calcAgain == "yes");
You might want to look at string operations for equality. The condition while(calcAgain == "yes") will not execute ever, because you are comparing the object itself. String equality is done with the .equals() function.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Equal strings aren't equal (==) in Java? [duplicate]
(3 answers)
Closed 9 years ago.
import java.util.Scanner;
public class NewClass {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name");
String name = scanner.nextLine();
System.out.println("Is "+ name + " really your name?");
String answer1 = scanner.nextLine();
if (answer1 == "yes"){
System.out.println("Alright ");
}else {System.out.println("Liar!");
}
}
}
It outputs Liar! even though I typed yes, so that answer1 equals yes. Why?
Because with == you are testing reference equality, not value equality. Good reading.
Instead answer1 == "yes" do answer1.equals("yes").