Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I created a url string array.
String urls[]={"http://www.kllhjj.png","http://yui.kl.png"};
this url are not exactly correct. But in my code them are correct. Then I try to convert them into integer array just like following way.
int a[] = new int[urls.length];
for(int i=0; i<a.length; i++) {
try {
a[i] = Integer.parseInt(urls[i]);
}
catch(Exception e) { }
}
But here always show the a integer values as 0. why is it?
help me
Because Integer.parseInt(urls[i]); is throwing NumberFormatException and you are swallowing the Exception . The below code will not work in your case, but at least you will get to know the error:
try{
a[i]=Integer.parseInt(urls[i]);
}catch(Exception e){
e.printStackTrace();
throw new RunTimeException(e);
}
All the elements of a primitive int array are defaulted to 0. Hence you get the 0s .
You cannot parse string like "http://yui.kl.png" etc to int as they are not in numeric format . Read the documentation:
Throws:
NumberFormatException - if the string does not contain a parsable integer.
You are getting the NumberFormatException Exception hence not changing any values in a[] and because the default values of integer is 0 you are having 0 for every element in a[]
Urls are text string and you parsing text to int:
a[i]=Integer.parseInt(urls[i]);
which through an exception absorbed by:
catch(Exception e)
and default value for int is zero so u always getting it.
ParseInt is a function that search for integer in some string. it's like atoi if you're familiar with c language.
When you have a string that contains only letters it will return 0.
When you have a string that begins with letter and ends with number it also will return 0.
It will convert a string to integer if the string will contains a number characters like "0123".
I don't know what you're trying to do, but if you want that number will represent a url.
I think you can create an enum for the urls.
Related
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 7 years ago.
A part of the goal in my app is to receive a mathematical expression (e.g. 1 + 1) by string, and convert it to a BigInteger.
My goal is to have an equivalent to:
BigInteger result = new BigInteger("1+1");
// This will throw an exception of invalid BigInteger
Also, ScriptEngineManager class isn't available for Android.
I still cannot find a way to achieve my goal.
Thanks a lot for helping!
You can try :
//If you have to parse only the result
String result = "2";
BigInteger.valueOf(Long.valueOf("2");
//If you have to parse the whole operation
String result = "1+1";
//create an algo to extract each 1 and determine operation
BigInteger bigIntUn = new BigInteger("1");
BigInteger bigIntAnotherUn = new BigInteger("1");
//you have add (+), min(-), multiply(*) (etc...)
bigIntUn.add(bigIntAnotherUn);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am developing an android application were I want to parse data through an array to URL API.
Taking an example, there are 5 TextBox and I enter some information in it. Then all the values entered in textView should parse in an array format to That API URL.
Please help!!
This is what i did, note that this is just an example.
final Map<String,String> postParam = new HashMap<String, String>();
for(int i = 0; i < 5; i++)
postParam.put("child_id[" + i + "]", i+"");
You will get :
child_id[0] with value 0
child_id[1] with value i
And goes on.
UPDATE
In your case, you might want to do something like :
postParam.put("child_id[" + i + "]", myEditText.getText.toString());
for each of your edittext.
Feel free to comment if you dont understand my answer or if i miss-understood you.
I hope i am understanding you correctly, you want to put 5 TextBox's entered text into one Array and then send this Array to API.
Try this:
ArrayList<String> textViewTexts = new ArrayList<String>();
// Put all EditText's text to array
// Do this for each EditText
textViewTexts.add(someEditText.getText());
You can then use textViewTexts.toString() and send this to API.
EDIT:
you can parse textViewTexts like this:
for (int i = 0; i < textViewTexts.size(); i++) {
String text = textViewTexts.get(i);
// Do something with text..
}
EDIT2:
you can parse textViewTexts like this:
JSONArray jArray=new JSONArray();
for (int i = 0; i < textViewTexts.size(); i++) {
String text = textViewTexts.get(i);
jArray.put(text);
}
// Send JSONArray to API
jArray.toString();
If you want to send that array to server via an api, then you should send data in JSONArray like this
JSONArray jArray=new JSONArray();
jArray.put(yourTextViewText1);
jArray.put(yourTextViewText2);
jArray.put(yourTextViewText3);
jArray.put(yourTextViewText4);
jArray.put(yourTextViewText5);
and you can send that to server like that
params.put("key",jArray.toString());
Moreover, It is easy for you web developer to parse this JSONArray.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I hope somebody could give me an explaination why the below code wont work:
//Why doesnt this work
String l = myString.substring(cut, lengthLastBtn-1);
String c = myString.substring(cut, lengthLastBtn-1);
if(l==c){
Log.i(TAG, "Correct");
}
//End
//This work!
String l = "hi";
String c = "hi";
if(l==c){
Log.i(TAG, "Correct");
}
// End
// Or if i want the Vars as in the first code i have to use the if statement like this
if(l.contains(c)){
Log.i(TAG, "Correct");
}
//End
So, why cant a compare a string when i have used the substring method on it. I even see in the log for the strings that they are the same, or have the same text at least.
When you use the “==“ operator with String`s, it means a comparison between objects, not the value that objects hold.
In order to compare Strings values , you should use the built-in method equals. The result is true if the String object represents the same sequence of characters.
if(string1.equals(string2)) {
//Match
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am getting Exception that json object cannot be converted to json array..,when I am showing values from server, here is my code. Please help me.
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("List of image URLs");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
Here is my json exception at this output
{
"message": [
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346961Quincredible_1-
2.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346977Quincredible_1-
3.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389346996Quincredible_1-
4.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347016Quincredible_1-
5.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347039Quincredible_1-
6.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347052Quincredible_1-
7.png",
"http:\/\/app.lionforge.com\/comics\/adminpanel\/upload\/1389347062Quincr32.png"
]
}
This data is coming from server. I don't know why this error is occurring, my images are not showing and moving.
Please help me. Thank you.
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
change to:
if(respons!=null){
try{
JSONObject jsonObj=new JSONObject(respons);
JSONArray post=jsonObj.getJSONArray("message");
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
}
key for your json array is message not List of image URLs
Change your below line in which you need to get the arraylist of message where you have written wrong key. Other code is fine.
JSONArray post=jsonObj.getJSONArray("List of image URLs");
Change it to
JSONArray post=jsonObj.getJSONArray("message");
As you are getting only the JSONArray in your response there is not need of JSONObject in your code.
You can parse your array as below which will directly give you an array :
try{
JSONArray post=new JSONArray(respons);
for(int i =0;i<post.length();i++){
String values = post.getString(i);
_issueList.add(values);
}
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Why does my "if" statement not fire?
String something = "";
String category = json_data.getString("den");
Log.e("JSON", "category="+category);
if (category == "1"){
something = "Random something";
}
Even if in my logcat I can see JSON:"category=1", the "something" String does not take "Random something" value.
This must be some convention in java?
Please help.
use:
if (category.equals("1")){
something = "Random something";
}
or better way to avoid null pointer exception:
if ("1".equals(category)){
something = "Random something";
}
Also have a look at this link for detail : How do I compare strings in Java?
You need to do string comparison in Java using the .equals method on the String object. The comparison you are doing is only comparing the references not the actual string values.
Example:
if(category.equals("1")){
//do amazing stuff
}