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

Java

Timothy Van Cauwenberge
Timothy Van Cauwenberge
8,958 Points

Code runs but gives wrong output!

The code is supposed to convert the amount of Yen given into U.S. dollars, but it gives out a slightly wrong answer. Please help. Code is pasted below:

public static void main(String [] args)    {
        Scanner input = new Scanner(System.in);
        int sentinel = 0;
        double yen = 1;
        // every .0089 cents is eqivalent to 1 japanese yen
        double conversion;
        while (sentinel != 1) {
            System.out.println("\nPlease enter an amount of Yen");
            yen = input.nextDouble();
            if (yen <= 0) {
            // if input is 0 or lower it doesn't convert because that's not an
            //amount of money
            conversion = yen;
            System.out.printf("Not valid amount of currency. Try Again.");
        } else if (yen >= 111.98) {
            // if the input is greater than or equal to 111.98 yen (1 U.S. dollar) the program
            // prints out only the first two decimals to keep it organized
            conversion = yen * .0089;
            System.out.printf("\nThat is %.2f U.S. dollars", conversion);
        } else if (yen < 111.98) {
            // If input is lower than 111.98 it prints out the first 4 decimals because 
            // 1 yen is .0089 cents in the US
            conversion = yen * .0089;
            System.out.printf("\nThat is %.4f U.S. dollars", conversion);
        }
      }
    }

Moderator edited: Added markdown to make the code render properly on the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Timothy! I added some markdown to your code to make it appear properly in the question. If you take a look at the "Markdown Cheatsheet" link at the bottom of the "Add an Answer" section, you can find instructions on how to apply markdown to your posts :sparkles:

Miguel Rivas
Miguel Rivas
4,902 Points

Hi Timothy, I haven't executed your code but since it speaks about currencies and convertion types of decimals you could have a quick read about the fact that you shouldn't be using 'double' nor 'float' for this task.

Instead try with BigDecimal (I see a main class in your code so I'm guessing this is not a challenge)

fonts: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency

Have a great day.

M.