Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

What is the best practice method to take user input (numbers) calculate with the input and display it back to the user.

Hello, Im building my first original app, which is a very simple tax calculating app that takes user input as numbers and adds the tax value then returns the result to the user. Im having trouble converting the input data into a double or int so that I can return the result. Thanks,

1 Answer

Hi Michael,

It might be helpful to others if you can post the code that you have so that the community can take a look and offer helpful hints and advice.

You don't need to return a numeric value, you just need numerics to perform your calculations and then you can convert that to a String so that you can update/set the text for your app. I did something very similar here: Discount Calculator Example.

    //Calculates and returns a String representation of how much the user
    //should pay
    public String calcPay() {
        double saved = userInput - (userInput * discount);
        formatter.format(saved);
        String youSaved = String.valueOf(formatter.format(saved));
        return "$" + youSaved;
    }
    //Calculates and returns a String representation of how much the user
    //saved

    public String calcSaved() {
        double amount = userInput * discount;
        String youPay = String.valueOf(formatter.format(amount));
        return "$" + youPay;
    }

I hope this helps :)