• 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

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
 
no I wasn't aware of that. thanks. I tried the code both with the parseDouble and with the parseInt, I got a syntax error for parseInt. and a runtime error with parseDouble
 
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) );
 
I am still getting that runtime error. Its saying its an exception. But i
I don't know anything about debugging. Should I just show you the entire code? Maybe I am doing something else wrong.
 
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 !
 
Back
Top Bottom