• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to make a string value into a integer

Lentor5847

Lurker
Sep 27, 2014
4
0
Hello everyone! I'm new here and I had a question. I have the following code:

public void onCheckedChanged(RadioGroup rg, int i){

if(i==t10.getId())
ta.setText(df.format(Double.parseDouble(ba.getText().toString())*.10));
td.setText(ta.getText().toString()+ ba);
if(i==t15.getId())
ta.setText(df.format(Double.parseDouble(ba.getText().toString())*.15));
td.setText(ta.getText().toString()+ ba);
if(i==t20.getId())
ta.setText(df.format(Double.parseDouble(ba.getText().toString())*.20));
td.setText(ta.getText().toString()+ ba);
}

I'm not too sure if your going to be able to see this well honestly...but I hope someone can help me with this. when I run the application it does not add the values of "ba" and "ta" it concatenates them instead. I don't know how to fix this. please help!
 
Hello everyone! I'm new here and I had a question. I have the following code:

public void onCheckedChanged(RadioGroup rg, int i){

if(i==t10.getId())
ta.setText(df.format(Double.parseDouble(ba.getText().toString())*.10));
td.setText(ta.getText().toString()+ ba);
if(i==t15.getId())
ta.setText(df.format(Double.parseDouble(ba.getText().toString())*.15));
td.setText(ta.getText().toString()+ ba);
if(i==t20.getId())
ta.setText(df.format(Double.parseDouble(ba.getText().toString())*.20));
td.setText(ta.getText().toString()+ ba);
}

I'm not too sure if your going to be able to see this well honestly...but I hope someone can help me with this. when I run the application it does not add the values of "ba" and "ta" it concatenates them instead. I don't know how to fix this. please help!

We have a little mess here :) but that's OK that's why forum is here.

1st You have to parseDouble every time when you want to use a string as a double value. So lines

td.setText(ta.getText().toString()+ ba);

should be like

td.setText(Double.parseDouble(ta.getText().toString()) + Double.parseDouble(ba.getText ().toString()));


2nd You are aware that only the first line after "if" line is executed if "if clause" is true? Second line is executed in any case. I guess you need some braces there.


also even if you allowed only numeric data in your fields you should check for an empty values. Otherwise you'll get an exception.

and just saw the title.. If you want int values then use parseInt and not parseDouble
 
Upvote 0
I guess you are seeing a syntax error because the parseInt function is a member of the Integer class so you need to call Integer.parseInt() and not Double.parseInt(). The runtime error is probably because there are 2 versions of the setText() function. One lets you set the text to a String/CharSequence object the other takes a resource id (an integer) and looks up the string from an XML file packaged with your app. You are probably calling the second version and it can't find any strings in your XML file with the matching resource id.

You can do something like this:
Code:
td.setText( Double.toString(Double.parseDouble(ta.getText().toString()) + Double.parseDouble(Double.toString(ba.getText().toString()))) );

// Or make it a bit more readable
Double sum = Double.parseDouble(ta.getText().toString()) + Double.parseDouble(Double.toString(ba.getText().toString()));
td.setText( Double.toString(sum) );
 
Upvote 0
This is my first message in this site, think i can solve it, i hope.

so the problem is comming from "ba.getText().toString()*.10" you can't multiply a string 10 times, you have to convert that string to numeric data (integer or flaot) the solution is :

int ba_int = Integer.parseInt(ba.getText().toString());
ta.setText(df.format(Double.parseDouble(bt_int*10));
td.setText(Integer.parseInt(ta.getText().toString()) + ba_int );

you can also use the same idea for flaot, just change int by float

Hope it work

Caramilleapp !
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones