This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm learning java and I have a problem:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
import java.util.*;
public class edytor{
public static void main(String[] args) throws FileNotFoundException
{
Scanner czynowy = new Scanner(System.in);
System.out.println("Do you wanna editing existing file?");
String tn = czynowy.nextLine() ;
if(tn=="t")
{System.out.println("bleble"); }
Scanner odpowiedz = new Scanner(System.in);
System.out.println("Type file name");
String polozenie = odpowiedz.nextLine() ;
System.out.println("################################");
PrintWriter zapis = new PrintWriter(polozenie);
Scanner tekst = new Scanner(System.in);
String tekst1 = tekst.nextLine() ;
zapis.println(tekst1);
zapis.close();
}
}
It's compiling, but
when in string tn I type t char this doesn't print "bleble". What should I do to make it work?
Greetings!
if (tn.equals("t") {...}
String is an object, and if you create 2 strings, even if they will have same value, they won't be equal to eachother
string1 == string2 // false
the == checks the object identity. while the .equals() method in String, checks the value.
The other way of doing this will be to use for loop, to loop through every char in each string, and check if it matches the character with the same place in another string.
or in your case, do this:
if (tn.getBytes()[0] == 't') {...}
You need to use
if (tn.equalsIgnoreCase("t") {
...
}
The reason you cannot compare two Strings with == is because Strings are objects. When you try to compare two objects directly, you are comparing their locations in memory. So, even if the contents of two Strings may be equal, their memory locations will not be equal.
Related
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)
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))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String[] alphabets={"a","b"};
String [] cipher={"b","c"};
System.out.println("Please enter a letter.");
String word=input.nextLine();
for (int i=0;i<2;i++){
if (word.equals(alphabets[i])){
System.out.println("Preparing a cipher");
System.out.println("Here is the cipher: "+cipher[i]);
}
}
}
}
This code above works perfectly fine, but instead of saying (word.equals(alphabets[i])), if i put word==alphabets[i] it wouldn't work at all. While using the later, the program does not check if the input is equal to a value in the array. Why does this happen?
== is an operator which compares the objects' locations in memory. equals() is a method defined in Object meant for comparing the actual content/values. By default, these two options behave similarly, but Java's String class overrides equals() when comparing strings.
In the future, please do research before asking questions. There is plenty of information to be found on the Internet.
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.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
Hello I am having a some difficulty with a very simple program.
import java.util.*;
public class Compare
{
public static void main( String args[] )
{
String username;
Scanner input = new Scanner(System.in);
String users[] = {"John", "James", "Smith", "Paul"};
System.out.println("Please Enter Username: ");
username = input.next();
for( int i = 0; i < users.length(); i ++ )
{
if( users[i] == username )
System.out.println("Match");
else
System.out.println("No Match");
}
}
}
When I run this program I get No Match which I don't know how is possible when I enter the same string as on of the strings in the users array. Any Suggestions?
use .equals method to compare strings ( and any other objects ) not == operator.
for instance:
users[i].equals(username);
== operator used with reference variables checks if they are pointing at the same spot in the memory.
You need to use users[i].equals(username).
== uses reference, not value comparison.
You need to write
if (users[i].equals(username))
Don't use == to compare Strings in Java, because it determines whether two Strings are the exact same object in memory; whereas equals actually compares the characters in the Strings.