equals method and single quote - android

I've this code:
gProvincia = "Reggio nell'Emilia";
if (gProvincia.equals(provincia.getProperty("provincia").toString()))
{
indiceSpinProv = x;
}
It seems that the single quote cause a problem to equals method, because setting gProvincia as Parma, equals return true when finds the same object in provincia.getProperty("provincia").
But this doesn't happen when the gProvince value contains a single quote.
I've also tried escaping the single quote with "\", without success.
Any suggestion?
Thanks!

Related

Add a number and a Text Input value with adobe flex

I am trying to add a number and a text input value to display in a label. here is my code thus far.
'lblAnswer.text = bloodglucose + 100;'
Please tell me what I am doing wrong.
Please try following answer -
bloodglucose += 100;
lblAnswer.text = String(bloodglucose);
Hope this will work :)
Sunil is correct - when doing mixed type addition, the UI input first needs to be coerced to either int or Number. IE: Number(bloodglucose) + 100; This assumes bloodglucose is actually a getter to the input text reference. If it's not, then you need to coerce the property and not the id of the component.
Getter: public function get bloodglucose():Number { return Number(myInput.text); }
In method: lblAnswer.text = bloodglucose + 100;
or (bloodglucose is a UIComponent):
In method: lblAnswer.text = Number(bloodglucose.text) + 100;
You should use String(int i)
lblAnswer.text = String(bloodglucose + 100);
Update: What about something like this:
var i:int = bloodglucose + 100;
var s:String = String(i);
lblAnswer.text = s;
** Update ,
I am changing the code from the update that was previously posted. I initially found that because I was including the string value inside of the equation this is what was prompting an error. You have to wrap the converted components to Number inside of the string all together. Basically convert the components to a number, then convert the answer received into a string.
Below is an example of the wrong code.
txtAnswer = (String(Number(bloodglucose)+100)) / 36)).toFixed(2)
Below this line is the fixed code.
txtAnswer.text = String( (Number(bloodglucose.text) + (Number(100))/ (Number(36))).toFixed(2) ;
The .toFixed Property signifies how many decimal places I want the returned value to display.

trim() on an empty EditText returns false android

i have the following
String newWord = (String) addNewWordEdTxt.getText().toString();
Log.d(TAG_WORD, "A:"+ String.valueOf(newWord.trim() == "" ));
Log.d(TAG_WORD, "B:" + String.valueOf( TextUtils.isEmpty(addNewWordEdTxt.getText().toString().trim() )));
anyone know why A is false and B is true for an empty EditText
thanks
anyone know why A is false and B is true for an empty EditText
Because in A case, you are comparing references and not quality so you have to do it like that:
Log.d(TAG_WORD, "A:"+ String.valueOf(newWord.trim().equals("")));
Note: There is one golden rule: If you want to compare Strings, always use equals() method!
Here is nice explanation:
How do I compare strings in Java?
i thinks java a string compare with any string use .equals() method not used ==
that's like
String.valueOf(newWord.trim().equals(""));

Program seems to be ignoring lines of code

I need help with this function.
I know that the if statement recognizes my input because it affects the program elsewhere, but I'm not sure what's going on because this particular Log doesn't display anything even in adb logcat.
Other Log statements in the same class file that this function is from display just fine, and the value update does seem to be changing ("show all" blanks it for some reason but I can figure that out after I get the log to work.)
I am unsure how to search for this problem because it is very specific and I have no idea what causes it (probably something simple that I didn't think of, though.)
void command(String input)
{
//do stuff here
//update = whatever
if(input.equalsIgnoreCase("show all"))
{
update=printAllRooms();
Log.i(input, update);
}
else update=input; //just for testing, will delete later
}
the printAllRooms function:
public String printAllRooms() //for debug purposes
{
String result = "";
for (Iterator<Room> iterator = rooms.iterator(); iterator.hasNext();) {
Room current = iterator.next();
result = result + current.toString()+"\n";
Log.i("printallrooms", current.toString());
}
return result;
}
A note on using Log.
The first argument sent to Log is typically a fixed string indicating the name of the class you are in.
So at the top of your class you might define:
private static final String TAG = "MyClassName";
Then you would use TAG for your log statements in that class.
Log.i(TAG, "My input was: " + input + " Update was: " + update;
To put it mildly, your function looks quite odd. Set a breakpoint at your Log statement, run the debugger and then inspect the variable value contained in update. Most likely, printAllRooms() is not doing what you think.
If the iterator doesn't work for you, try using the For-Each loop:
for (Room r : rooms) {
result = result + r.toString()+"\n";
Log.i("printallrooms", r.toString());
}

Converting String with toCharArray results in different Characters

This is baffling me. I am grabbing a String and converting it to a Char array but the resulting characters are not the same as the original String. What gives? I've tried it one character at a time as well as trying toCharArray(). Same results.
Output:
07-21 09:58:27.700: V/meh(22907): Loaded String = [C#42126d88
07-21 09:58:27.700: V/meh(22907): Convert to Char = [C#41693070
String temp = prefManager_.getString("PrevGameState", "");
Log.v("meh", "Loaded String = " + temp);
pieceStates_ = temp.toCharArray();
Log.v("meh", "Convert to Char = " + pieceStates_.toString());
The value it outputs is not a string indeed, it's a pointer in memory. Probably you are not overriding the toString() method or there is something wrong.
The fact that the two pointers are not the same doesn't mean that the two strings are not equal (which should be compared with .equals(..) and not in any different way).
To be more precise, if pieceStates_.toString() prints [C#41693070 then the toString is not overridden and Java doesn't know how to print it. Same thing applies to the other variable. Then an array type in Java is not printable by default, you should use Arrays.toString(..) to actually see its content.
Use :
System.out.println("Convert to Char = " + String.valueOf(pieceStates_) );
String.valueOf(Character_Array)
Above method converts it back to String object.

Compare two things doesn't work

I have a strange problem in my android app. I must compare two string which are equals. I tried this :
if (raspunsdata.equals(rok)) {
System.out.println("changed ");
} else
System.out.println("no change");
}
but I get always "no change". Before this I have System.out.println for both strings, and both of them have the same value.
I tried also (raspunsdata==rok) and raspunsdata.contentEquals(rok) but I have the same problem. Why? I cant understand this.,...please help...
You might have unwanted white spaces. Might need to use the trim function just to make sure.
if (raspunsdata.trim.equals(rok.trim())) {
System.out.println("changed ");
} else
System.out.println("no change");
}
Btw equals is the correct way to check whether the values are the same.
.equals - compares the values of both objects. If you have 2 Strings with the same characters sets .equals will return true;
== - compares if two objects references are equal.
For example:
String a = "lol";
String b = a;
a == b - will be true.
Try reading: http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml

Categories

Resources