filepath comparsion through if condition [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am getting the two different path that are basically file path , injected through xml by spring ioc..
private String FilePath1; //it consists path c:\abc folder
private String FilePath2; //it consists of c:\abc\def
now I need to check that if they two are equal then they should go inside the
condition otherwise not
please advise how to achieve this..
what I have tried is ..
if (FilePath1 =! FilePath2)
{
filemove(mcrpFilePath, zipfileName); //i want if two file paths are not wqula then it // should go inside filemove
}

Use equals() to compare Java Strings. The reason for this is because Strings are objects in Java. Therefore, the == operator tests to see if two objects point to the same instance! not whether they are equal to each other.

Related

text.compareTo() what does this means? [duplicate]

This question already has answers here:
Why should a Java class implement comparable?
(10 answers)
Closed 6 years ago.
String ntext;
ntext = something;
String currentLine;
currentLine = something;
while(ntext.compareTo(currentLine) != 0){
//some condition
}
Here i want to know what that compareto actually do.
One more questin what we can use to compare two objects?
If those variables are strings (i assume so) it checks if they are equal, returns 0 if so, and another number if not. See the JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
More specifically, it goes through both strings character by character. When it finds a string of one that is not equal to the other, it returns a number representing whether the differing character is more than, less than, or equal to the corresponding character in the other string.

Simple check operation is not working in java [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality

Weird Java extended ascii error [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to do a comparison in Java with 2 strings containing a extended ASCII character.
boolean result = "éasdfasdf".substring(0,1).equals("é");
Can somebody explain why this results false? I think it has something to do with character encoding, but I can't figure out what exactly the problem is here...
Update: ideone.com does successfully run these 2 lines, so the problem is locally in my box. I think I found some more proof of that:
System.out.println("éb".charAt(1) == 'b');
Does also fails... Can it be the problem of 2 different character encodings?
Use
boolean result = "éasdfasdf".substring(0,1).equals("é")
And it will give expected result!The reason is simple - using '==' you compare objects by reference, not by value. So equals() solves this problem

How can I do a presence check in Java? i am getting an error. [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i am trying to do a presence check in java on 10 JTextFields. I want it so that if all 10 of my textfield have something in them, it will do my code.
String input1 = tfQ1.getText();
String input2 = tfQ2.getText();
etc.
I have put
IF(input1==("")&&input2==("")&&input3==("")&&input4==("")&&input5==("")&&input6==("")&&input7==("")&&input8==("")&&input9==("")&&input10==(""))
{
//DO SCORES ETC
}
However, this doesnt do anything... (my button does not work weather there are things in the text fields or not)
Please and someone help with presence check validation? Thanks =)
instead of "==" operator you should use
input1.equals("")
if(input.equals("WhateverYouAreLookingFor")) {
//do this
}else {
//do this
}
== is a reference comparison, both objects point to the same location in memory. essentially it tests wether the two operands refer to the same object.
.equals() will only compare what is in the String. It can be overridden so two distinct objects can still be equal.

Two strings are equal but wont work on if statement [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
System.out.println(characters.get(selected).getName());
// The name printed is "Mario" But the if statement will not
// work as though the two strings are diffrent but they are the same.
if(characters.get(selected).getName() == "Mario"){
playSound("sounds/clickmario.wav");
}
Comparing two strings and when I debug the comparison is "Mario" to "Mario" so the if statement should be true but its false because nothing inside the if statement is being read. Why is this happening? I have tried assigning this .getname to a tempString and comparing it but still when they are the same string the statement results as false. Please help
You have to use .equals() for string comparison in java
if(characters.get(selected).getName().equals("Mario")){
playSound("sounds/clickmario.wav");
}
Refer this for String comparison.
and
for basics of String refer this.
use
if (stra === strb)
it should work in javascript, for java
if (stra.equals(strb))
Then it should work too

Categories