Android - String to Integer then Integer to increase the String value - Json - android

String value comes like float value, then I need to convert it into integer after that I want increment integer value means string will automatically get new string from j-son. How to handle this problem ?

Try this :
String stringValue = "2.3";
int intValue = Integer.valueOf(stringValue);
intValue++;
stringValue = String.valueOf(intValue);
I don't my IDE with me so maybe you should have to add try/catch etc.

From String to float use
float f = Float.parseFloat("1235.00"); // replace with your string
From String to int use
int i = Integer.parseInt("741125"); // replace with your string
From int or float to String use
String s = String.valueOf(yourIntOrFloat);
For increment use
i++;
or
i=i+1;

Related

Convert String same as into Int in Android

I want to convert String into int.
For example:
if String is "0x1F60A" then it should be convert into int same as 0x1F60A
String str = "0x1F60A";
int i = 0x1F60A; // it must be something like this.
Why I need this. Because I'm going to convert unicode int into Emoji
I'm using this code.
final String emojiString = str.substring(start,end);
// value in emojiString is 0x1F60A
int unicode = // need to convert this string into int
ss.replace(start, end, new String(Character.toChars(unicode)));
Can you please let me know how can I do that.
For conversion of Unicode to Int:
You have to omit the 2 first characters with .substring(2)
int code = Integer.parseInt(unicodeStr.substring(2), 16);
For conversion of String to int:
try {
myNum = Integer.parseInt(myString);
} catch(NumberFormatException nfe) {
}
int unicode = Integer.parseInt(emojiString.substring(2), 16);

How to get split string with special characters in android?

I have a string named namely "-10.00","-100.00","-1000.00". I want to get value like "10","100","1000" from that string. I have tried to get substring but did not able to get.
code i have tried
String amount = "-10.00";
String trimwalletBalance = amount.substring(0, amount.indexOf('.'));
From above i only get "-10".
String trimwalletBalance = amount.substring(1, amoun.indexOf("."));
Its very simple.
Do it like String trimwalletBalance = amount.substring(1, amount.indexOf('.'));
Instead of position 0, You should get substring from position 1
Convert it into integer and then name it positive:
String amount = "-10.00";
int amountInt = (int) Double.parseDouble(amount);
if(amountInt<0)amountInt*=-1;
Try
String amount = "-10.00";
int value = (int) Double.parseDouble(amount);
if(value < 0) value *= -1;
//value will be 10
OR
String text = amount.substring(1, amount.indexOf('.'));

getstring() giving null value in jsonparsing

I am getting following response from server
{"RESPONSE":"ATTENDANCE","STATUS":"SUCCESS","projid":1,"invalid_labours":[]}.
i wrote following code of parsing
JSONObject jsonObj = new JSONObject(attendanceResponse);
String response = jsonObj.getString("RESPONSE);
String status = jsonObj.getString("STATUS);
String projectID = jsonObj.getString("projid");
JSONArray invalidLaborId = jsonObj.getJSONArray("invalid_labours");
but its giving nullpointer while fetching projid field
Replace
String projectID = jsonObj.getString("projid");
with
int projectID = jsonObj.getInt("projid");
Also it seems you are missing a double quote...
-------------------------------------------------------------------------\/
String response = jsonObj.getString("RESPONSE");
Replace
String projectID = jsonObj.getString("projid");
with
int projectID = jsonObj.getInt("projid");
projid has an integer value, try the following if you want a String :
String projectID = String.valueOf(jsonObj.getInt("projid"));
when you are try to get value from JSONObject try to use optXXX() method
in your case you are projectID is int so
int projectID = jsonObj.optInt("projid");
Difference between optXXXX() and getXXXX() method
optInt()
Get an optional int value associated with a key, or zero if there is no such key or if the value is not a number. If the value is a string, an attempt will be made to evaluate it as a number.
getInt()
Get the int value associated with a key.
This method throws: JSONException - if the key is not found or if the value cannot be converted to an integer.

How to split this String?

I want to split this string
String info = "0.542008835 meters height from ground";
from this i want to get only two decimals like this 0.54.
by using this i am getting that
String[] _new = rhs.split("(?<=\\G....)");
But i am facing problem here, if string does't contain any decimals like this string
String info = "1 meters height from ground";
for this string i am getting those characters upto first 4 in the split string like 1 me.
i want only numbers to split if it has decimals, How to solve this problem.
if(info.contains("."))
{
String[] _new = rhs.split("(?<=\\G....)");
}
I think you can check by white space after first value. see this
If you get the space then get first character only.
For checking if a string contains whitespace use a Matcher and call it's find method.
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
If you want to check if it only consists of whitespace then you can use String.matches:
boolean isWhitespace = s.matches("^\\s*$");
You could use a regex to do this as an alternative to Deepzz's method, this will handle the case where there is a '.' in the later part of the String, I've included an example below. It's not clear from your question is you actually want to remaining part of the String, but you could add a second group to the reg ex to capture this.
public static void main(String[] args) {
final String test1 = "1.23 foo";
final String test2 = "1 foo";
final String test3 = "1.234 foo";
final String test4 = "1.234 fo.o";
final String test5 = "1 fo.o";
getStartingDecimal(test1);
getStartingDecimal(test2);
getStartingDecimal(test3);
getStartingDecimal(test4);
getStartingDecimal(test5);
}
private static void getStartingDecimal(final String s) {
System.out.print(s + " : ");
Pattern pattern = Pattern.compile("^(\\d+\\.\\d\\d)");
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println(matcher.group(1));
} else {
System.out.println("Doesn't start with decimal");
}
}
Assuming the number is always the first part of the string:
String numStr = rhs.split(" ")[0];
Double num = Double.parseDouble(numStr);
After that you can use the String Formatter to get the desired representation of the number.
This will work when you know the String near the numbers, with int and double numbers as well.
String a ="0.542008835 meters height from ground";
String b = a.replace(" meters height from ground", "");
int c = (int) ((Double.parseDouble(b))*100);
double d = ((double)c/100);

int to String in Android

I get the id of resource like so:
int test = (context.getResourceId("raw.testc3"));
I want to get it's id and put it into a string. How can I do this? .toString does not work.
String testString = Integer.toString(test);
Or you can use Use
String.valueOf()
eg.
int test=5;
String testString = String.valueOf(test);
Very fast to do it if you dnt remember or dnt have time to type long text :
int a= 100;
String s = ""+a;
What about:
int a = 100;
String s = String.format("%d", a);

Categories

Resources