Comparing character of an array to a string [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
What's the best way to compare character of an array to a string.
import java.io.Console;
class msd{
public static void main(String[] args){
Console console= System.console();
String letter="Hello";
String[] Arrayname=letter.split("");
if(Arrayname[0] == "H")
console.printf("success");
else
console.printf("failure");
}
}
Output
failure
Expecting to be success.

You have to use the equals method to do so:
Arrayname[0].equals("H")
The == operator compares references not values.

Related

How do I correctly get the value of 'ⅻ' (and other similar non-alphabetical characters) and store it in a string? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
Given a character that is not a standard alphabet character, such as 'ⅻ', I'm having problems converting it to a string and retaining it's value. For example If I have:
String myStr = "ⅻⅾ℡ℬ";
Character myChar = myStr.charAt(0);
Then System.out.println('ⅻ' == myChar); returns true, whereas System.out.println("ⅻ" == Character.toString(myChar)); returns false.
Thus my question effectively is how do I correctly get the value of 'ⅻ' and store it in a string?
Both of these conditions return true:
public class Test {
public static void main(String args[]) {
String myStr = "ⅻⅾ℡ℬ";
Character myChar = myStr.charAt(0);
System.out.println('ⅻ' == myChar);
System.out.println("ⅻ".equals(Character.toString(myChar)));
}
}

java strings inmutable but the code doesn't shows that [duplicate]

This question already has answers here:
Immutability of Strings in Java
(26 answers)
String is immutable. What exactly is the meaning? [duplicate]
(19 answers)
Closed 4 years ago.
I was learning string concepts, so wrote a code,expected a different output but got something very unexpected.
class stringmute
{
public static void main(String[] args)
{
String s1="Hello "; //string one.
System.out.println("Str1:"+s1);
String s2= s1+"world"; //New String.
System.out.println("Str2="+s2);
s1=s1+"World!!"; //This should produce only Hello right?
System.out.println("Str1 modified:"+s1);
}
}
when I execute the above code i get the output as:
Str1:Hello
Str2=Hello world
Str1 modified:Hello World!!
if i've done something wrong please let me know.
Since strings are immutable, which implies we should get the output of the "Str1 Modified" as "HELLO" instead of "HELLO WORLD!!".
When you assign s1 as :
s1=s1+"World!!";
New String created in jvm string pool and assigned to s1.
So it's value became "Hello World!!"

Why Don't These Strings Match? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm getting the user input like so:
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
When I compare it like this
if(input == "abc"){
System.out.println("Match!");
}else{
System.out.println(input + "\nabc");
}
If I type "abc" I see
abc
abc
Instead of the expected match. What's going on?
You are comparing String references, not String values. Use the String.equals method instead.
== is comparing memory locations. you need to use input.equals("abc") instead.

Why I new the object not equal to the String? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String s1 = "abcdef";
String s3 = new String(s1);
if(s3 == s1){
System.out.printf("yes");
}else{
System.out.printf("no");
}
Why is not print yes? Who can explain to me?
Change s3 == s1 to s3.equals(s1).
The == operator or will check if they're both the same object, rather than what their string value is.
When you are dealing with Objects, you should use their equals method, not the ==
Check this for further explanation:
Java String.equals versus ==

Error on checking if value of textField is equal to string JAVA [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
Hey im having trouble with checking if a JTextField value is given. I think I have the correct code, but it is not saying anything that could possible be broken.
Q: Why wont it display or run the Game.main(null) when the value of the textfield is "Dianamu" ?
public void mouseClicked(MouseEvent e) {
String values = textField1.getText();
if(values == "Dianamu"){
Game.main(null);
}
System.out.println("Login Works:");
}
Thanks in Advance
if(values.equals("Dianamu")){
Game.main(null);
}
EXPLANATION:
== compares object references; .equals() compares values

Categories