What does getText().toString() return? - android

I am new to Android. Please help me to clear about the code below:
String num1 = etfirst.getText().toString();
String num2 = etsecond.getText().toString();
String keyword is used for characters, but here its used for receiving the numbers.
Please explain about the dot operator and getText().toString().
Thanks in advance.

Here you are getting text from etfirst and ersecomd and convert it into string using toString().
getText(): Return the text that is displaying.
toString(): Returns a string representation of the object.

Related

Android : Split string with { character

I have an string
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,"
I want only this 46199005.
But { shows an error, when try to split the string
String[] separated = name.split("edge_followed_by\":{\"count\":");
Showing a suggestion , number expected and want me to replace with *.
Can anyone help me in this.
Just replace { with \{.
split is trying to use it as a part of regular expression.
Ideally, you should use JSON to parse this if you have proper structure. but if you want to get only the number you can split it using ":" and then split using "}". it should give you the exact number.
Why not to use:
String[] separated = name.split(":");
separated[2].split("}")[0];
Your string is not exact JSON object otherwise you can simply do json parsing and get the count value.
You can get count value using subtring operations like below:
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,";
String substr = name.substring(name.indexOf("\"count\":") + 10);
String finalstr = substr.substring( 0, substr.indexOf("},"));
Log.d("Extracted_Value", finalstr); // output -> 46199005
There can be multiple ways. This is just one. Hope it will help you!

Cannot resolve substring() or indexOf() methods

I am new to android and i am trying to search for a certain character in an edit text and then extract all the characters before it in a new variable using the substring() and indexOf() methods , but android studio saying that it cannot solve either method . So please tell me what i am doing wrong with the code. Here is the declaration for the edit text :
EditText text;
text = (EditText) findViewById(R.id.text);
And the code for calling these methods :
if(text.getText().toString().contains("+")) {
String before = text.substring(text.indexOf("+") - 1);
}
You're calling substring() and indexOf() on your text variable, which is an EditText object, and they're string methods. Convert to string first and then use them, like so:
String textString = text.getText().toString();
if(textString.contains("+")) {
String before = textString.substring(textString.indexOf("+") - 1);
}
You need to call them in the same way you call contains(): text.getText().toString() because the two methods are part of the String class, not EditText.
You can try String before = text.split("+")[0]; This will return you everything before the first '+'

The method getText() is undefined for the type string or mismacth cannot convert from void to string

I have
pesan.setText(_cipher.Encrypt(pesan.getText().toString()));
when i change to
String pesan = text.setText(_cipher.Encrypt(text.getText().toString()));
It become type of mismatch cannot convert from void to string. Please someone help me resolve this problem...
Try this it may help you:
String pesan =_cipher.Encrypt(text.getText().toString());
text.setText(""+pesan);
String pesan1=(text.getText().toString());
First understand about the return types.
Return type of setText is void, so you cant assign it to a String.
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence,android.widget.TextView.BufferType)
Otherwise you can do like this:
text.setText(_cipher.Encrypt(text.getText().toString()));
String pesan = text.getText().toString();
Hope this may help you.

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

Not able to get name/value pairs from JSON object, when using variable

Not able to get name/value pairs from JSON object, when using the variable but able to read it when hard coding the name.
To better explain :
1) My JSON object is like this -
{.....
{ "rates":{ "name1": value1, "name2": value2 ...etc }
...}
2) I am able to read this object in my android app.
3) Now this rate object name value pairs, i am trying to read based on user input -
String s1 = '"'+name1+'"'; // here name1 i got from user input, & converted into string
4) Now when i am trying to get the value from rates object, i am getting null exception -
JSONObject rateObject = jObject.getJSONObject("rates"); //able to get
complete object
String rate1 = (String) rateObject.get(s1); // giving NULL exception
5) But if i use hard code string, it works -
String rate1 = (String) rateObject.get("name1"); // working
Any pointers why its not working while using variable.
thanks
Thanks for suggestions, i sorted out the problem. There are 2 mistakes i was doing - 1) Using the quotes as correctly pointed out by others and 2) casting the double value to string. Correcting both has resolved my problem :)
In terms of your final code snippet, you are actually doing
String rate1 = (String) rateObject.get("\"name1\""); //note the extra quotes
because you have bookended the user input string with double-quote characters. You just want the input string itself with no bookending. The quotes in the JSON notation serve to delineate each key name; the quotes are not part of the key name itself.
You need to omit the quotes when you create s1:
String s1 = name1;
Or, if name1 is not a String already:
String s1 = name1.toString();
Replace:
String s1 = '"'+name1+'"';
with:
String s1 = name1;

Categories

Resources