Android - parse String longitude to float causes error - android

I need to convert my CMS data (which are provided as Strings) to float value, but I am getting exception
NumberFormatException: invalid float value: "16.385837"
The code looks like:
Double.valueOf(myString.trim()).doubleValue();
I've also tried like this:
Double.parseDouble(myString).doubleValue();
but i'm getting the same message. Do you have any idea what is wrong ?!

try {
String s = "16.385837";
Double d = Double.parseDouble(s);
System.out.println(d);// which will prints 16.385837
} catch (NumberFormatException e) {
// p did not contain a valid double
}

String s = e1.getText().toString();
Float f= Float.parseFloat(s);
use this code this will helps you
put your value on place of s; then you can parse string to float

Try this,
Double.parseDouble(String.valueOf("16.385837"));

try
Double.parseDouble(myString) not .doubleValue();

Try this
try {
Double d = Double.parseDouble(String.valueOf("16.385837"));
System.out.println(d);
} catch (NumberFormatException e) {
// Handle The Exception During Parsing
}

Related

Android - JSON No value for 1 - but there is using Log

I'm stuck at trying to get a simple string from a JsonObject.
Here is the basic code. result is a JsonObject returned from an Asynctask. I've done dozens of the same way, and here it doesn't work.
Response:{"status":200}
Error: An error is thrown saying "No value for 1"
When I Log the content of the JsonObject, I get this: 200, which is what I want. For which reason could this return null when there is a value?
Edit if I use the result.has("status"); true is returned. I don't understand.
Log.e("TAG", result.get("status").toString());
try{
String status = result.getString("status");
} catch (Exception e){
e.getMessage();
}
You are getting int value in the object and you are holding in string which causes the error
You are getting int value in status
{
"status":200
}
if it is like
{
"status":"200"
}
Your code works perfectly.
Try this
Log.e("TAG", result.get("status").toString());
try{
int status = result.getInt("status");
} catch (Exception e){
e.getMessage();
}

Get unmatched string from regex (Android)

Pattern p = Pattern.compile(".*\\\"(.*)\\\".*");
Matcher m = p.matcher("\"Hi there\"! How are you");
if (m.find()) {
try {
String iGotMyMatchedString = m.group(0);
} catch (Exception e) {
e.printStackTrace();
}
}
Above code will return me string inside double quotes which is "Hi there".
How can i get the remaininig string "!How are you"
You can not get the unmatched string, instead you can use a capture group around the second part and access to it by calling the group(2) on your matched object:
".*\\\"(.*)\\\"(.*)"

Eclipse - Functions issue [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to get text from server and then check it a to know what actions to take with the text adopted. The problem is that when I try to check if the received text for example is "Exited" the query always return the value "false" when the received text is really "Exited".
Here is the code :
class Get_Message_From_Server implements Runnable
{
public void run()
{
InputStream iStream = null;
try
{
iStream = Duplex_Socket_Acceptor.getInputStream();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//Create byte array of size image
byte[] Reading_Buffer = null;
try
{
Reading_Buffer = new byte [Duplex_Socket_Acceptor.getReceiveBufferSize()];
//New_Buffer = new byte [100];
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] Byte_Char_1 = new byte[1];
int Byte_String_Lenght = 0;
//read size
try
{
iStream.read(Reading_Buffer);
String Reading_Buffer_Stream_Lenghtor = new String(Reading_Buffer);
//System.out.println("full : " + Reading_Buffer_Stream_Lenghtor);
Byte_String_Lenght = Reading_Buffer_Stream_Lenghtor.indexOf(new String(Byte_Char_1));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//Convert to String
Meassage = new String(Reading_Buffer);
Meassage = Meassage.substring(0, Byte_String_Lenght);//The text that received
Message_Getted = 1;
}
}
The query :
if(Message_1 != "Exited")//the message query
{
System.out.println("Continued 253");
continue;
}
Its always return the value - false
its important to know that the message is in Utf - 8 encoding
so how i can to fix the issue ?
If you compare strings by using oparators, Java will not look at the contents of the string but at the reference in memory. To compare String content in Java, you should use the following:
String Message_1; // Hopefully has a value sent by the server
if(Message_1.equals("Exited")) {
// Do stuff when exited
} else {
// Do stuff when not exited
}
String is a variable - and variables should start with lower Case letter - Please read Java Code conventions. Also to check if your message contains string you thing it should just do System.out.println(Message_1); and if the message contains what you expect you compare string doing
if(Message_1.equals("Exited")) {
System.out.println("Yes they are equal");
} else {
System.out.println("No they are not");
}
If this will print "No they are not" that simply means that your variable Message_1 is not what you think it is.. As simple as that. There is no such a thing as .equals method does not work. Its your variable that doesn't ;)

unable to convert EditText to int using parseInt

The logcat shows that the Integer.Parse() function was not able to convert the string to int or float whereas i have linked the xml file in java. Also, i have set the input Type of the rate and quantity column as number so there is no chance that the user can enter anything other than an int or float. Plz help. I have been trying to get this code working for a long time. But i cant find the solution.
Attached is the code:
public int initializeVars(){
int i;
for(i=0;i<9;i++){
//items[i] is the AutoCompleteTextView array
item[i]=items[i].getText().toString();
iquant[i]=Integer.valueOf(quant[i].getText().toString());
irate[i]=Float.valueOf(rates[i].getText().toString());
if(item[i]=="")
break;
}
return i;
}
}
To avoid these kind of circumstances use these two functions.
For Int
private int getIntFromString(String str){
try{
return Integer.parseInt(str);
}catch(Exception e){
return 0;
}
}
For float
private float getFloatFromString(String str){
try{
return Float.parseFloat(str);
}catch(Exception e){
return 0f;
}
}
Call these methods at required places
Try to use the parseInt method instead of valueOf and also keep that parsing code in try{}...catch{} block of NumberFormateException.
try{
iquant[i]=Integer.parseInt(quant[i].getText().toString());
irate[i]=Float.parseFloat(rates[i].getText().toString());
}
catch(NumberFormateException e){}
INT
try {
myInt = Integer.parseInt(myString);
} catch(NumberFormatException e) {
}
FLOAT
try {
myFloat = Float.parseFloat(myString);
} catch(NumberFormatException e) {
}
Maybe instead of valueOf you can try parsing it directly via
yourInt = Integer.parseInt(yourString);
youtFloat = Float.parseFloat(yourString);
I always do it this way and it works. In the float variable make sure that decimals are marked by "." not ",".
You may also want to try and catch NumberFormatException to avoid problems while parsing.

Using fractions on android editText

I'd like an inputType in the edit text that i can input fractional number like this: 2/4 (i want print the "/").
The program is about calculating things and i need to type fractional insted of decimal. Thanks. Sorry my bad english.
I think the best way would be to use a string for your input text, then to parse the string to figure out what kind of things the user entered.
When the user has finished their input, you can check the string with something like this:
public float testInputString(String testString) {
boolean goodInput = true;
float result = 0;
if (testString.contains("/")) {
//possible division
String pieces[] = testString.split("/");
if (pieces.length != 2) {
goodInput = false;
} else {
try {
float numerator = Float.parseFloat(pieces[0]);
float denominator = Float.parseFloat(pieces[1]);
result = numerator/denominator;
} catch (Exception e) {
goodInput = false;
}
}
} else if (testString.contains(".")) {
try {
result = Float.parseFloat(testString);
} catch (Exception e) {
goodInput = false;
}
}
//TODO something here if bad input, maybe an alert or something
return result;
}
Also, you can check while they are typing for valid input if you use a keylistener like this. You could modify that to allow only numbers . and /.
put this property in xml node of Edittext and use Double notation instead of "/"
android:digits="1234567890.-"

Categories

Resources