1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to make a string value into a integer

Discussion in 'Android Development' started by Lentor5847, Sep 27, 2014.

  1. Lentor5847

    Lentor5847 Lurker
    Thread Starter

    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!
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Welcome to Android Forums.

    I moved your question to the forum where development questions are discussed.

    ... Thom
     
  3. Lentor5847

    Lentor5847 Lurker
    Thread Starter

  4. spectri

    spectri Lurker

    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
     
  5. Lentor5847

    Lentor5847 Lurker
    Thread Starter

    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
     
  6. GeorgeN

    GeorgeN Well-Known Member

    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 (Text):
    1.  
    2. td.setText( Double.toString(Double.parseDouble(ta.getText().toString()) + Double.parseDouble(Double.toString(ba.getText().toString()))) );
    3.  
    4. // Or make it a bit more readable
    5. Double sum = Double.parseDouble(ta.getText().toString()) + Double.parseDouble(Double.toString(ba.getText().toString()));
    6. td.setText( Double.toString(sum) );
    7.  
     
  7. Lentor5847

    Lentor5847 Lurker
    Thread Starter

    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.
     
  8. caramilleapp

    caramilleapp Lurker

    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 !
     
Loading...

Share This Page

Loading...