Decimal removal in calculations


Last Updated:

  1. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    Hi. I am working on a counting app and when it counts I want it to show a whole number with no decimal place. I'm not sure how to do this so any suggestions are welcome. Thanks.
     

    Advertisement
  2. gorn

    gorn Well-Known Member

    Joined:
    Mar 22, 2010
    Messages:
    48
    Likes Received:
    11
  3. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    If you are using Doubles, then your results will show up as decimals. You can either use DecimalFormat to make your result show up "decimal-less" or you can just simply use ints instead of Doubles? For example below represents difference between int and Double...

    int tests = 10;
    Double grade1 = 89.7;

    System.out.println("There were " + tests + " tests taken.");
    System.out.println("The highest grade in this class was " + grade1 +"%.");

    10 tests were taken but 89.7% was the highest. The Double, grade1, is with decimals. If we just wanted numbers without decimals, we would have to define a variable for type int, like tests, which the number of tests is 10.

    Do you understand?
     
  4. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    i think i get the intent thing but i dont believe that will work with what i am doing. How does the DecimalFormat work? Here is my current code for what im working with with the decimals

    private OnLongClickListener subtractRight = new OnLongClickListener() {
    public boolean onLongClick(View v) {
    Double scoreRight = Double.parseDouble(TextView02.getText().toString());
    Double addOne = Double.parseDouble(TextView03.getText().toString());

    {Double scoreLeftTotal = (scoreRight - addOne);

    TextView02.setText(scoreLeftTotal.toString());
    }

    return true;
    }
    };

    its like that in like four different area, giving different code for stuff like adding and subtracting points
     
  5. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    You see, you are parsing the "TextView02.getText().toString()" so of course your results are going to be returning decimals, silly! lol DecimalFormat is quite simple, but why don't you just use ints instead of Doubles?? or can't you just parse the TextView02.getText().toString()?

    Integer.parseInt(TextView02.getText().toString());

    that should remove the decimals...

    BTW, why are you parsing the TextView02.getText().toString()? if you already have the values set to Double? (scoreRight and addOne) You're parsing them as Doubles so of course a decimal value will be returned. :p
     
  6. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    however, I may be wrong because i still don't know what your whole "program"'s function so i don't know what we're dealing with. DecimalFormat is usually for when you show numbers(doubles) and they have like for example 4.5234523895230948, you can use it to get rid of those numbers and just have 4.5, 4.52, 4.523, etc. OR, you can just set it to just show 4( i believe). In our course, we just used ints because they don't have decimals...
     
  7. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    i did it that way because i am new to java and to android and pretty much all forms of coding, and its the one way i knew i could make equations. My app is like a score keeper for like a soccer game. I tried changing up some of the stuff like you said but im having some trouble getting the app to calculate anything. going to try some more on it. thanks for the help so far.
     
  8. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    Just to save you some time; the most simplest way to calculate numbers is using ints because when it comes to mixing up doubles and ints in "formula/calculation" methods, you'll get confused. Parsing and all that stuff is unecessary if you just want plain numbers. However, if you must like the display of let's say a soccer score is "4.0 to 3.0", then I suppose doubles would be an option, but I would never use doubles for something like what you're doing. :) How much experience do you have with Java?
     
  9. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    i know like zero java. i only know what ive figured out over the past year. recently got some android coding books though that ive been looking at some. here is my code currently. Im still getting errors though. the app keeps crashing. thanks for the help.

    package score.keeper;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnLongClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class Score extends Activity {

    private Button Button01;
    private Button Button02;
    private Button Button03;
    private Button Button04;
    private TextView TextView01;
    private TextView TextView02;
    private TextView TextView03;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button01 = (Button) findViewById(R.id.Button01);
    Button02 = (Button) findViewById(R.id.Button02);
    Button03 = (Button) findViewById(R.id.Button03);
    Button04 = (Button) findViewById(R.id.Button04);
    TextView01 = (TextView) findViewById(R.id.TextView01);
    TextView02 = (TextView) findViewById(R.id.TextView02);
    TextView03 = (TextView) findViewById(R.id.TextView03);



    Button01.setOnLongClickListener(subtractLeft);
    Button02.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func1();}});
    Button03.setOnLongClickListener(subtractRight);
    Button04.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func2();}});

    }


    void my_func1() {
    int scoreLeft;
    int addOne;
    int scoreLeftTotal;

    addOne = Integer.parseInt(TextView03.getText().toString());
    scoreLeft = Integer.parseInt(TextView01.getText().toString());

    scoreLeftTotal = (scoreLeft + addOne);

    TextView01.setText(scoreLeftTotal);
    }


    void my_func2() {
    int scoreRight = Integer.parseInt(TextView02.getText().toString());
    int addOne = Integer.parseInt(TextView03.getText().toString());

    {int scoreRightTotal = (scoreRight + addOne);

    TextView02.setText(scoreRightTotal);
    }

    }
    private OnLongClickListener subtractLeft = new OnLongClickListener() {
    public boolean onLongClick(View v) {
    int scoreLeft = Integer.parseInt(TextView01.getText().toString());
    int addOne = Integer.parseInt(TextView03.getText().toString());

    {int scoreLeftTotal = (scoreLeft - addOne);

    TextView01.setText(scoreLeftTotal);
    }

    return true;
    }
    };
    private OnLongClickListener subtractRight = new OnLongClickListener() {
    public boolean onLongClick(View v) {
    int scoreRight = Integer.parseInt(TextView02.getText().toString());
    int addOne = Integer.parseInt(TextView03.getText().toString());

    {int scoreLeftTotal = (scoreRight - addOne);

    TextView02.setText(scoreLeftTotal);
    }

    return true;
    }
    };

    }
     
  10. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    ok...it is saying that R cannot be resolved...I'm sorry but i don't understand why this is occurring. I'm really sorry. I've looked and looked everywhere on the developer.android site but can't find a solution :-/ If you post on this forum maybe something like "R cannot be resolved", maybe you can get more help. Otherwise, your program seems to be coded nicely, other than the R errors in your buttons/textviews. Best of luck, please ask if anything else is needed.
     
    Amon Darthir likes this.
  11. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    well, the hard thing is, im not getting an R error , im getting like an operation error. When i click on one of the buttons in my app the application stops unexpectedly, and i have no idea why. If you come up with anything let me know. Thanks
     
  12. fabiocberg

    fabiocberg Active Member

    Joined:
    Apr 7, 2010
    Messages:
    27
    Likes Received:
    1
    Hi,
    If your app stops unexpectedly, it's because happens some exception.
    You can find this exception in the Logcat window.
    If you put here the content of Logcat or a screenshot of it, I can help you.
     
  13. Christophe2010

    Christophe2010 Active Member

    Joined:
    May 13, 2010
    Messages:
    35
    Likes Received:
    4
    he can't even run it because it keeps crashing, which means it doesn't work. R cannot be resolved in his coding. We don't know how to fix this. His R.java file looks fine but somewhere there is something wrong...lol I don't think it's anything to do with his "logcat window".
     
  14. fabiocberg

    fabiocberg Active Member

    Joined:
    Apr 7, 2010
    Messages:
    27
    Likes Received:
    1
    Dear Christophe2010,
    If you had read the post above from mine, you could read he saying "im not getting an R error" and he write that "When i click on one of the buttons in my app the application stops unexpectedly"... So, he can run his application, and when click on one button he gets an error... So, this error will appear on Logcat.
    Regards
     
  15. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    Amon,

    Stop listening to Chris2010 - fabiocberg is right. If you're getting a Force Close, then you need to check your LogCat window for detailed information on why. I've induced an error in one of my projects and attached a screenshot below to help you find your error.

    Also, when you cut and paste code into your posts, it's much easier to read (and therefor help you) if you wrap your text in the "code" tags (you can use them by clicking # on the post message toolbar and inserting your code between them).

    Your LogCat window will be filled with all the log information that your program has dumped while your program is running. So when you get the Force Close, follow these instructions: In the screenshot below, you'll notice you need to open the Debug perspective first (top right red circle). Then maximize the LogCat window (I've shown mine as the top left red circle, but it'll probably be on the lower right for you and might even be minimized with a picture of the Android droid or the words "LogCat"). Then you'll notice the text in red showing the exact type of error that caused the Force Close (indicated by the arrow - and in this case a NullPointerException), and the exact method and line # in your project which caused the error (in this case line #75 in my onCreate() method). This information should lead you directly to the problem.

    If you have any further problems, please ask.

    [​IMG]
     

    Attached Files:

  16. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    since you asked here is the logcat lot of good this will probably do me but maybe you can do something with it huh
     

    Attached Files:

  17. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    Well it looks like the root of the error is at line 53 in your application in a method called my_func1.

    What's happening is that your program is trying to reference a string resource (located in the /values/strings.xml) and it can't find it. The resource id that's being called for is 0x1.

    So, there's at least 2 things which could be happening just off the top of my head:

    1) The string resource identifier that's being passed to setText(id) is incorrect. I've seen it before where people try to pass a resource id that isn't a string resource. That is, someone might use: R.id.something, instead of R.string.something. It should look something like:
    Code (Text):
    1. textView.setText(R.strings.my_string_resource);
    2) You've recently deleted a string resource and the generated resources haven't refreshed themselves (so eclipse still thinks the resource exists and lets your program run anyway). Make sure to refresh your project. Right click on the project and click "Refresh".

    Those are the two big issues which I think might be the problem, in order of most likely. Do you understand how to read your LogCat window? The information is there, but sometimes takes a little experience on how to read correctly. You should be able to find all that information that I just gave you (basically) from those red lines in your LogCat window.

    --Boogs
     
  18. fabiocberg

    fabiocberg Active Member

    Joined:
    Apr 7, 2010
    Messages:
    27
    Likes Received:
    1
    Boogs is right.
    The error is because some string resource at line 53 cant be found.
    But, Amon, at post number #9 you put your code like this:

    void my_func1() {
    int scoreLeft;
    int addOne;
    int scoreLeftTotal;

    addOne = Integer.parseInt(TextView03.getText().toString());
    scoreLeft = Integer.parseInt(TextView01.getText().toString());

    scoreLeftTotal = (scoreLeft + addOne);

    TextView01.setText(scoreLeftTotal);
    }

    Here you arent using any string resource, have you changed it?
    But I think when you refresh your project you will find the error.
     
  19. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    Ah... that appears to be the most logical problem; you're calling setText with an int value:
    Code (Text):
    1. TextView01.setText(scoreLeftTotal);
    where, scoreLeftTotal is an "int" value. The interpreter is expecting any int value to be a reference to a string resource. I'd bet dollars to doughnuts that either one of these will fix your issue:
    Code (Text):
    1. TextView01.setText(scoreLeftTotal.toString());
    or
    Code (Text):
    1. TextView01.setText("" + scoreLeftTotal);
     
  20. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    Wow. I cant believe that was it. Thanks everyone for the help. I tried Boogs second suggestion first and it worked like a charm. Ill try your first suggestion also just to see what happens. Thanks everyone for the help.
     
  21. Amon Darthir

    Amon Darthir Active Member This Topic's Starter

    Joined:
    Feb 4, 2010
    Messages:
    27
    Likes Received:
    0
    no luck on the first one because it says that its not a primative type and no string is declared kinda thing so the second one worked. Thanks. :D
     
  22. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    I'm glad it worked for you. And, my bad, the first example would have worked if it was an "Integer" object instead of an "int".

    Boogs
     
  23. fabiocberg

    fabiocberg Active Member

    Joined:
    Apr 7, 2010
    Messages:
    27
    Likes Received:
    1
    You also could use this: TextView01.setText(Integer.toString(scoreLeftTotal));
     

Share This Page

Loading...