This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I want to ask a question.
I feel so confused about my own coding because i think it is correct.
This is the issue.
public static void main(String[] args) {
String x = "Robert : Hi There";
String y = "Robert";
System.out.println(x.substring(0, x.indexOf(":")).trim());
if(x.substring(0, x.indexOf(":")).trim() != y){
System.out.println("Pass");
}
else
{
System.out.println("Not Pass");
}
}
This gave me output:
Robert
Pass
I want the output is "Not Pass" but why did my coding gave another result.
I hope you can tell what is wrong.
Thank You.
You compare string objects. So you have to ue the equals method:
if(x.substring(0, x.indexOf(":")).trim().equls(y)){
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
Bellow is snippet of my code. I just want to know why if condition is not being executed even if variable "ans" has value 2020.
private static void solve() throws Exception {
String str="20";
String str1="20";
String ans=str+str1;
if(ans=="2020")
System.out.println("matched");
else System.out.println("Not matched");
System.out.println(ans);
}
ans.equals("2020")
this should work.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 5 years ago.
I'm trying to return the array of String but I keep returning [Ljava.lang.String;#1909752 as my answer. Could experts please kindly provide some suggestions to solve this? Thanks in advance!
public String[] getCardNames()
{
String[] namesInHand = new String[CARDS_IN_HAND];
for (int i =0; i < CARDS_IN_HAND; i++)
{
if (hand[i] != null)
{
namesInHand[i] = hand[i].toString();
}
}
return namesInHand;
}
That is the expected behaviour for printing an array. If you want it in a human readable format, call Arrays.toString():
System.out.println(Arrays.toString(getCardNames()))
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i want to ask a question for a bedsize and while the answer is not what i choose it will be i want that it will ask the user to answer again
import java.util.Scanner;
public static void main(String[] args) {
String newBedType ;
Scanner sc1 = new Scanner(System.in) ;
System.out.println("you want a single bed or doublebed? ") ;
newBedType = sc1.next() ;
while (newBedType != "single" + "doublebed") {
System.out.println("please choose againe the bed size: ");
newBedType = sc1.next() ;
switch (newBedType) {
case "single" : System.out.println("i see you like sleeping alone");
break ;
case "doublebed" : System.out.println("got company ;) ");
break ;
}
}
}
}
the code kinda works it shows the cases if i write the correct string but it will continue to ask me forever.....
i just stared learning java so be easy on me i know its a stupid question but after hours of trying and searching here(though i did found in python but dont know how to "translate" it to java)
i cant figure it out... thanks to anyone willing to help :)
Your issue is with the line:
while (newBedType != "single" + "doublebed")
This doesn't do what you think it does. You are comparing the variable newBedType with the string "singledoublebed", the addition operator is concatenating those two strings. You want the line:
while (!newBedType.equals("single") && !newBedType.equals("doublebed"))
Note the use of the .equals() method, as string comparisons in Java do not act as expected with the == or != operators.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I don't understand what's wrong with the IndexOf function ???
public String[] PseudoExisteTest() {
// looking if an XML tag contain "OK"
String exampleText = "<result>OK</result>";
int ind1;
int ind2;
String returnTable[] = new String[4];
String tag="result";
String textresult;
ind1=exampleText.indexOf("<"+tag+">"); // 0
ind2=exampleText.indexOf("</"+tag+">"); // 10
textresult=exampleText.substring(ind1+tag.length()+2, ind2);
if ((textresult=="OK")) { // YES => Normally we pass here (="OK") !
returnTable[0]="It'OK";
}
else {
returnTable[0]="Not, value is : "+textresult+"!"; // Not, value is : OK !!! ?????
}
returnTable[1]="blabla";
return returnTable;
}
The value is "OK" but on the condition, that's don't works well ????
Is anybody can help me ?
Thanks in advance.
The problem is that you're using == to do a comparison of Java Strings. For objects in Java, which includes Strings, == tests whether the objects are the same. Instead, say textresult.equals("OK") or textresult.equalsIgnoreCase("OK").
As the comments say, see also How do I compare strings in Java?
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
For some reason, my (basic) program always prints the text I reserved for my else statement.
I am a newb when it comes to Java, so if I am making an obvious mistake I apologize. I also searched high and low for an answer, but couldn't find one. Could you take a look at this:
package test;
import java.util.Scanner;
public class tutorial_7 {
private static Scanner x;
public static void main(String args []) {
x = new Scanner(System.in);
System.out.print("Apples, or oranges: ");
String bog = x.next();
if (bog == "Apples") {
System.out.print(1);
}
if (bog == "Oranges") {
System.out.print(2);
}
else {
System.out.print(3);
}
}
}
}
Why is the text reserved for my if statements never being output? Everything seems to be fine.
Regards,
JavaNoob
Don't use == to compare strings, it's for object identity.
Comparing strings should be done with the equals() method, such as:
if (bog.equals ("Oranges")) {
How do I compare strings in Java?
if (bog.equals("Apples")){
System.out.print(1);
}
if (bog.equals("Oranges")){
System.out.print(2);
}
else{
System.out.print(3);
}