IF condition on strings [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I'm writing a simple code to test the value that was inputted to my constant value.
I declared this code as my constant value.
String LetMeThrough = "drunk";
String GotAnID = "drunk";
This is the whole code.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner DrunkTest = new Scanner(System.in);
String InputDrunk;
String InputDrunkAgain;
String LetMeThrough = "drunk";
String GotAnID = "drunk";
System.out.print("Type drunk: ");
InputDrunk= DrunkTest.next();
System.out.print("Re Type drunk: ");
InputDrunkAgain = DrunkTest.next();
if(InputDrunk == LetMeThrough & InputDrunkAgain == GotAnID){
System.out.print("You're not DRUNK");
}
else
System.out.print("You're F***** DRUNK");
}}
The problem is that if I type "drunk" on both.
I will get "You're F****** DRUNK" instead of the "You're not DRUNK".
When the inputted values is the same as my constant values.

You must use String::equals method to compare.

Related

how to comapare string to a variable [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed last year.
I want to know how to compare string to a variable.
For example I want to check if the input taken is run and if it's not print that is not a command
class Main
{
public static void main(String args[])
{
System.out.println("a pokrmon appeard");
System.out.println("what would you like to do");
Scanner scan = new Scanner(System.in);
Random genrator = new Random();
String input =scan.nextLine();
int genrate;
genrate=genrator.nextInt(4);
String fail ="failed to escape";
String escaped ="got away safly";
if (genrate <= 2){
System.out.println(escaped);
}
else{
System.out.println(fail);
}
};
I have tried using some methods like if(input=="run") but it doesn't work
Salam (Hello in Muslim) guy.
To compare a string with another string, you need to call the string1.equals(string2) method.
It compares the value of string1 with string2 and if the values match, it returns true otherwise false.
string1==string2 - compares references to objects of type string, not the values of these objects. You can read more about this here How do I compare strings in Java? . Good luck learning Java. (I'm from Russia, I'm writing from a translator)

string variable in if statment fail [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 1 year ago.
so I try to use a string variable in the if statement but it seems it doesn't work like int and double and other primitive data types, why? Pls help
my code:
import java.util.Scanner;
public class Something{
static void Some(){
Scanner input = new Scanner(System.in);
String answer;
System.out.println("Hello Sir, what can I do for you?");
answer = input.nextLine();
String i = "2";
if(answer == i){
System.out.println("Sure Sir ");
}
else{
System.out.println("Sir, anything?");
}
}
public static void main(String [] args){
Some();
}
}
input:2
output: Sir, anything?
The == operator compares references in case of strings. To compare values you can use equals() method.
if(a.equals(b)){}

Java - IF condition not reading entered String variable value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String play = "y";
System.out.print("Enter something: ");
play = scan.next();
System.out.println(play);
if (play == "Y" || play == "y")
{
System.out.println("If test works!!");
}
System.out.println("Did it work???");
}
}
I assume this has something to do with when I press enter, it's storing that as well. I tried changing String play to a char, but then I get errors from Scanner saying it can't change a String to a char.
You should atmost avoid using “==“ when comparing objects especially strings. “==“ checks for object references. Change the comparison to use .equals method and it should work
if(play.equals(“Y”) || play.equals(“y”))
in case if “play” can be null, the below snippet is more safe.
if(“Y”.equals(play) || y.equals(play))

After splitting array of strings conditions of the array elements are always false [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Storing array of strings in specifies variables respectively
(2 answers)
Closed 6 years ago.
#DavidMarciel the code is now supposed to store the received value in the corresponding variable after checking that some condition is true, but when the program doesn't enter the body of if-condition even though the condition is true. I put an example in this code for the "male" variable being true, and placed a print statement inside the body of the if to show it. Below is my code.
public class j {
static String sss = "male,O+,45,saudi,brain_diseases";
static String male = "";
static String blood = "";
static String age = "";
static String nat = "";
static String dis = "";
static void func() {
String[] pieces = sss.split(",");
male = pieces[0];
blood = pieces[1];
age = pieces[2];
nat = pieces[3];
dis = pieces[4];
System.out.println(male);
System.out.println(blood);
System.out.println(age);
System.out.println(nat);
System.out.println(dis);
//
if(male=="male"){
System.out.println("hello male");
}
}
public static void main(String[] args) {
func();
}}
Replace
if(male=="male"){
with
if (male.equals("male")) {
The operator == test to check if two objects are the same objects. Instead the method .equals check for the content of the strings in this case.

Need to fix program in Java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want user to input text while it is not equal to "start".When it is equal to "start" I want to show "Bravo".In my code when I enter "start" it just continue to ask to input a text.What is missing in my code to process the operation i described.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(komanda != "start");
System.out.println("Bravo");
}
}
You have to use the equals method to compare strings in java:
while (!komanda.equals("start"));
or even better
while (!"start".equals(komanda));
this does not crash if komanda is null
See How do I compare strings in Java? for more information.
do it this way
Scanner input = new Scanner(System.in);
String komanda = "a";
do {
System.out.println("Unesi komandu ");
komanda = input.nextLine();
}
while(!"start".equals(komanda));
System.out.println("Bravo");

Categories

Resources