Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Incorrect output and other problems

  1. #1
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Incorrect output and other problems

    Skip to //what bills
    import javax.swing.*;
    import java.math.*;
    import java.text.*;
    import java.util.*;
    public class Subway{
        public static void main(String[]  args){
            DecimalFormat df = new DecimalFormat("#.##");
            String title1 = "How Many Monies?";
            String title2 = "This Many Monies!";
            String title3 = "Cash Drop Helper";
            final double minus = 200;
            //Gather Variables
            String userInput = JOptionPane.showInputDialog(null, "How many 100's are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double hundred = Double.parseDouble(userInput);
            hundred = hundred * 10000;
     
            userInput = JOptionPane.showInputDialog(null, "How many 50's are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double fifty = Double.parseDouble(userInput);
            fifty = fifty * 5000;
     
     
            userInput = JOptionPane.showInputDialog(null, "How many 20's are there?", title1,  JOptionPane.QUESTION_MESSAGE);
            double twenty = Double.parseDouble(userInput);
            twenty = twenty * 2000;
     
            userInput = JOptionPane.showInputDialog(null, "How many 10's are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double ten = Double.parseDouble(userInput);
            ten = ten * 1000;
     
            userInput = JOptionPane.showInputDialog(null, "How many 5's are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double five = Double.parseDouble(userInput);
            five = five * 500;
     
            userInput = JOptionPane.showInputDialog(null, "How many 1's are there?", title1, JOptionPane.QUESTION_MESSAGE);        
            double one = Double.parseDouble(userInput);
            one = one * 100;
     
            userInput = JOptionPane.showInputDialog(null, "How many quarters are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double quarter = Double.parseDouble(userInput);
            quarter = quarter * 25;
     
            userInput = JOptionPane.showInputDialog(null, "How many dimes are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double dime = Double.parseDouble(userInput);
            dime = dime * 10;
     
            userInput = JOptionPane.showInputDialog(null, "How many nickels are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double nickel = Double.parseDouble(userInput);
            nickel = nickel * 5;
     
            userInput = JOptionPane.showInputDialog(null, "How many pennies are there?", title1, JOptionPane.QUESTION_MESSAGE);
            double penny = Double.parseDouble(userInput);
     
            //Add Variables
            double dollars = hundred + fifty + twenty + ten + five + one;
            double cents = quarter + dime + nickel + penny;
            double total = dollars + cents;
            total = total / 100;
            JOptionPane.showMessageDialog(null, "Total is $" + total, title2, JOptionPane.INFORMATION_MESSAGE); 
            //Correct all the variables
            hundred = hundred / 100;
            fifty = fifty / 100;
            twenty = twenty / 100;
            ten = ten / 100;
            five = five / 100;
            one = one / 100;
            quarter =  quarter / 100;
            dime = dime / 100;
            nickel = nickel / 100;
            penny = penny / 100;
     
            //Display each amount
            final String[] eachAmt = {"Money in hundreds: $" + hundred, "Money in fifties: $" + fifty, "Money in twenties: $" + twenty, "Money in tens: $" + ten, "Money in fives: $" + five, "Money in ones: $" + one, "Money in quarters: $" + df.format(quarter), "Money in dimes: $" + df.format(dime), "Money in nickels: $" + df.format(nickel), "Money in pennies: $" + df.format(penny)};
            JOptionPane.showMessageDialog(null, new JList(eachAmt), title3, JOptionPane.INFORMATION_MESSAGE);
     
            //Subtract 200
            double TAKE = total - minus;
     
            String takeOut = "Take out " + TAKE + "\nPut it in an envelope." + "\nDrop it in the red safe.";
            JOptionPane.showMessageDialog(null, takeOut, title3, JOptionPane.INFORMATION_MESSAGE);
     
            //what bills
            int take = TAKE;
            double tHundreds = 0;
            double tFifties = 0;
            double tTwenties = 0;
            double tTens = 0;
            double tFives = 0;
            double tOnes = 0;
     
            if (take >= 100){
                tHundreds = take / 100;
                take = take % 100;
            }
            if (take >= 50){
                tFifties = take / 50;
                take = take % 50;
            }
            if (take >= 20){
                tTwenties = take / 20;
                take = take % 20;
            }
            if (take >= 10){
                tTens = take / 10;
                take = take % 10;
            }
            if (take >= 5){
                tFives = take / 5;
                take = take % 5;
            }
            tOnes = take;
            final String[] bills = {"Take Out" + "\n" + tHundreds + " Hundreds" + "\n" + tFifties + " Fifties" + "\n" + tTwenties + " Twenties" + "\n" + tTens + " Tens" + "\n" + tFives + " Fives" + "\n" + tOnes + " Ones"};
            JOptionPane.showMessageDialog(null, bills, title3, JOptionPane.INFORMATION_MESSAGE);
        }
    }
    the problem does not occur until //what bills. I can't use int as the data type because of the 'loss of accuracy' as the terminal states when I attempt a compile. The code does not give me correct, whole number amounts. I also need it to tell me how many quarters etc. to take.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect output and other problems

    I can't use int as the data type because of the 'loss of accuracy'
    Can you give an example using real numbers of what you are trying to do?

    Do you know how to cast?
    int x = (int) (12/14.0);  //  cast double result to int
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect output and other problems

    take = 158.49
    I need it to tell me that I should take out
    1 Hundred
    1 Fifty
    0 Twenty
    0 Ten
    1 Five
    3 One
    1 Quarter
    2 Dime
    0 Nickel
    4 Penny

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect output and other problems

    You've posted the desired final output of the program.
    What are the individual steps the code must take to get it?

    Take it one step at a time.
    How do you get: 1 Hundred
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect output and other problems

    I would see how many times 100 could fit into take and use that number.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect output and other problems

    I would see how many times 100 could fit into take and use that number.
    Something like this? 1 = 158.49 / 100
    Ok, now show the java code that will do that.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect output and other problems

    int tHundreds = (int) take / 100;

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect output and other problems

    You need to wrap the whole expression in ()s otherwise the cast will only be on take, unless that is all you want to have cast.

    What happens when you compile and execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect output and other problems

    so it should read: int tHundreds = int(take / 100);

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect output and other problems

    What happens when you compile and execute the code?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect output and other problems

    it doesn't compile

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect output and other problems

    Quote Originally Posted by lanmonster View Post
    it doesn't compile
    Please copy the full text of the error messages and paste them here.
    Also paste the new code.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Incorrect output and other problems

    You have a line that says:

    double TAKE = total - minus;

    then later it tries to use the same variable for an int:

    int take = TAKE;

Similar Threads

  1. Please tell me what i did incorrect
    By fh123boys in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 27th, 2012, 11:30 AM
  2. Incorrect results
    By kprofgold in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2012, 08:08 PM
  3. Problems with input/output and getters/setters
    By robbiep551 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: January 5th, 2012, 11:44 PM
  4. Having problems with output to console....help!
    By toppcon in forum What's Wrong With My Code?
    Replies: 21
    Last Post: July 13th, 2011, 06:38 PM
  5. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM