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 8 of 8

Thread: Brand Spanking New to Java...

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Brand Spanking New to Java...

    Hi everybody,

    As the subject says, I'm brand new to Java, new to all programming languages for that matter, and I'm also brand new to these forums. I know, the new guy is already asking for help

    This is my code so far: My "Output" isn't complete, and don't currently need help there.
    public class Banking {
     
    	public static void main(String[] args) 
    		{
     
    		System.out.println("Break money amount into the lowest number of bills possible");
    		int hundreds, fifties, twenties, tens, fives, twos, ones;
    		int remainingAmount;
     
    		//Input
    		String input = JOptionPane.showInputDialog("Enter the amount");
    		hundreds = Integer.parseInt(input);
    		fifties = Integer.parseInt (input);
    		twenties = Integer.parseInt(input);
    		tens = Integer.parseInt(input);
    		fives = Integer.parseInt(input);
    		twos = Integer.parseInt(input);
    		ones = Integer.parseInt(input);
    		remainingAmount = (hundreds/100) + remainingAmount;
     
    		//Computation
    		hundreds = input/100;
    		int totalBills=0;
    		totalBills += hundreds + %;
    		fifties = remainder/50;
    		totalBills += fifties;
    		twenties = remainder/20;
    		totalBills += twenties + %;
    		tens = remainder/10;
    		totalBills += tens + %;
    		fives = remainder/5;
    		totalBills += fives + %;
    		twos = remainder/2;
    		totalBills += twos + %;
    		ones = remainder/1;
    		totalBills += ones;
     
    		//Output
    		System.out.println()
     
    		}
     
    }

    I couldn't figure out how to upload my .java file
    Last edited by forte; May 16th, 2010 at 02:46 PM. Reason: Please use code tags.
    Sorry, I'm new


  2. #2
    Junior Member
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Brand Spanking New to Java...

    Well, nobody has responded, but that is Ok.

    My last bit of code was completely wrong.

    I was able to figure out approximately 90% of what I have to accomplish (all by myself I might add )

    But I am stuck once again. I can't seem to figure out how to get the proper result from the application. I get the popup "Enter Amount" in which I enter in whatever $ amount, but my output doesn't show how many hundreds, fifties, twenties, etc... was needed to compute the $ amount entered

    here is my revised code:

    import javax.swing.JOptionPane;
     
    public class Banking
    {
     
    	public static void main(String[] args) 
    	{
     
    	System.out.println("Break amount into the lowest number of bills possible");
    	int hundreds, fifties, twenties, tens, fives, twos, ones, amount;
    	int remainingAmount;
     
    	//Input
    	String input = JOptionPane.showInputDialog("Enter amount");
    	amount = Integer.parseInt(input);
    	hundreds = Integer.parseInt(input);
    	fifties = Integer.parseInt(input);
    	twenties = Integer.parseInt(input);
    	tens = Integer.parseInt(input);
    	fives = Integer.parseInt(input);
    	twos = Integer.parseInt(input);
    	ones = Integer.parseInt(input);
    	remainingAmount = amount/hundreds + fifties + twenties + tens + fives + twos + ones;
     
    	//Computation
    	int remainder;
    	hundreds = amount/100;
    	remainder = remainingAmount%100;
    	fifties = amount/50;
    	remainder = remainingAmount%50;
    	twenties = amount/20;
    	remainder = remainingAmount%20;
    	tens = amount/10;
    	remainder = remainingAmount%10;
    	fives = amount/5;
    	remainder = remainingAmount%5;
    	twos = amount/2;
    	remainder = remainingAmount%2;
    	ones = amount/1;
     
    	//Output
    	System.out.println ("Requested Amount (amount)");
    	System.out.println ("hundreds + (hundreds)");
    	System.out.println ("fifties + (fifties)");
    	System.out.println ("twenties + (twenties)");
    	System.out.println ("tens + (tens)");
    	System.out.println ("fives + (fives)");
    	System.out.println ("twos + (twos)");
    	System.out.println ("ones + (ones)");
     
    		}
     
    }

    So after running that, the popup comes up and ask for my "Enter amount" and then the result is this:

    Break amount into the lowest number of bills possible
    Requested Amount (amount)
    hundreds + (hundreds)
    fifties + (fifties)
    twenties + (twenties)
    tens + (tens)
    fives + (fives)
    twos + (twos)
    ones + (ones)

    So how do I change my code so that all of the "()" displays the amount entered instead of "amount, hundreds, fifties, etc"
    Sorry, I'm new

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Brand Spanking New to Java...

    If I understand your question correctly, you need to parse your integers into Strings (see Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics) for the basics of primitives). In your case, you can use Integer.toString() method, for instance

    String fivesAsString = Integer.toString(fives);
    System.out.println("fives + (" + fivesAsString + )");

  4. The Following User Says Thank You to copeg For This Useful Post:

    forte (May 17th, 2010)

  5. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Brand Spanking New to Java...

    You need to look at the input code - you're getting a single value from the user and parsing it into all the variables (amount, hundreds, fifties, twenties, tens, fives, twos, one) so they all contain the same value. Is that what you want? (if so, just parse it once and copy into the other variables. If not, change it).

  6. #5
    Junior Member
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Brand Spanking New to Java...

    I'm pretty sure what you guys said helped, but I can never know for sure. Every time I add/change something I get a new or another error.

    I've updated my code....again with your suggestion, but now I have more issues.

    package cs520.hw1;
     
    import javax.swing.JOptionPane;
    public class Banking {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
     
    		System.out.println("Break amount into the lowest number of bills possible");
    		int hundreds, fifties, twenties, tens, fives, twos, ones, amount;
    		int remainingAmount;		
     
    		// Retrieve amount to calculate
    		String input = JOptionPane.showInputDialog("Enter the amount");
    		amount = Integer.parseInt(input);
     
    		hundreds = (amount  (amount % 100))/100;
    		remainingAmount = amount%100;
     
    		fifties = (remainingAmount  (remainingAmount % 50))/50;
    		remainingAmount = amount%50;
     
    		twenties = (remainingAmount  (remainingAmount % 20))/20;
    		remainingAmount = amount%20;
     
    		tens = (remainingAmount  (remainingAmount % 10))/10;
    		remainingAmount = amount%10;
     
    		fives = (remainingAmount  (remainingAmount % 5))/5;
    		remainingAmount = amount%5;
     
    		twos = (remainingAmount  (remainingAmount % 2))/2;
    		remainingAmount = amount%2;
     
    		ones = remainingAmount;
    		remainingAmount = 0;
     
    		// Display the calculation to the user
    		System.out.println ("Requested Amount ( + Integer.toString(amount) + )");
     
    		System.out.println ("hundreds ( + Integer.toString(hundreds) + )");
     
    		System.out.println ("fifties ( + Integer.toString(fifties) + )");
     
    		System.out.println ("twenties ( + Integer.toString(twenties) + )");
     
    		System.out.println ("tens ( + Integer.toString(tens) + )");
     
    		System.out.println ("fives ( + Integer.toString(fives) + )");
     
    		System.out.println ("twos ( + Integer.toString(twos) + )");
     
    		System.out.println ("ones ( + Integer.toString(ones) + )");
     
    	}
     
    }

    Now I get an error under "packagecs520.hw1", "import javax.swing.JOptionPane;" and
    "public class Banking {"

    I feel like I'm close to resolving this, but then again I thought that before too.
    Sorry, I'm new

  7. #6
    Junior Member
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Brand Spanking New to Java...

    To add some clarification, I'm looking for my code to do something like this:

    Input popup

    "Enter the amount:"_______

    (393 for example)

    The console output would look like this:

    "hundreds = 3, remaining amount = 93"
    "fifties = 1, remaining amount = 43"
    "twenties = 2, remaining amount = 3"
    "tens = 0, remaining amount = 3"
    "fives = 0, remaining amount = 3"
    "twos = 1, remaining amount = 1"
    "ones = 1"

    Dialog output would look like this:

    Requested Amount (393)
    Hundreds (2)
    fifties (1)
    twenties (2)
    tens (0)
    fives (0)
    twos (1)
    ones (1)

    Hope that helps if anyone is trying to figure out what I'm asking/talking about.
    Last edited by forte; May 17th, 2010 at 02:14 PM.
    Sorry, I'm new

  8. #7
    Junior Member
    Join Date
    May 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Brand Spanking New to Java...

    OK, so I am almost complete with this. I feel like I have been spending every waking second on this simple/stupid application

    Anyway, I have but 2 issues/questions:

    1. My math/remainder/remaining amount must be off. Fore example, if I enter in "464", I get "hundreds (4), fifties (1), twenties (0), tens (0), fives (0), twos (2), ones (0)" which only equals 454....I don;t get it

    2. I need to display the dialog output that shows:

    Requested Amount (x),
    hundreds (x)
    fifties (x)
    twenties (x)
    tens (x)
    fives (x)
    twos (x)
    ones (x)

    and I'm not sure what the commands would be for this. I tried "string output = JOptionPane.showOutputDialog ("Message") - but that didn't work

    package cs520.hw1;
     
    import javax.swing.JOptionPane;
     
    public class Banking {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		System.out.println("Break amount into the lowest number of bills possible");
     
    		int hundreds, fifties, twenties, tens, fives, twos, ones, amount;
    		int remainingAmount;
     
    		// Retrieve amount to calculate
     
    		String input = JOptionPane.showInputDialog("Enter the amount");
    		amount = Integer.parseInt(input);
     
     
    		hundreds = (amount - (amount % 100))/100;
     
    		remainingAmount = amount%100;
     
     
    		fifties = (remainingAmount - (remainingAmount % 50))/50;
     
    		remainingAmount = amount%50;
     
     
    		twenties = (remainingAmount - (remainingAmount % 20))/20;
     
    		remainingAmount = amount%20;
     
     
    		tens = (remainingAmount - (remainingAmount % 10))/10;
     
    		remainingAmount = amount%10;
     
     
    		fives = (remainingAmount - (remainingAmount % 5))/5;
     
    		remainingAmount = amount%5;
     
     
    		twos = (remainingAmount - (remainingAmount % 2))/2;
     
    		remainingAmount = amount%2;
     
     
    		ones = remainingAmount;
     
    		remainingAmount = 0;
     
    		// Display the calculation to the user
     
    		System.out.println ("Requested Amount (" + Integer.toString(amount) + ")");
    		System.out.println ("hundreds (" + Integer.toString(hundreds) + ")");
    		System.out.println ("fifties (" + Integer.toString(fifties) + ")");
    		System.out.println ("twenties (" + Integer.toString(twenties) + ")");
    		System.out.println ("tens (" + Integer.toString(tens) + ")");
    		System.out.println ("fives (" + Integer.toString(fives) + ")");
    		System.out.println ("twos (" + Integer.toString(twos) + ")");
    		System.out.println ("ones (" + Integer.toString(ones) + ")");		 
     
    		}
     
     
    	}
    Last edited by forte; May 17th, 2010 at 07:21 PM.
    Sorry, I'm new

  9. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Brand Spanking New to Java...

    In your calculations, you should be using the 'remainingAmount' to calculate each time, not using the 'amount' which is always what was originally input.

    If you had stepped through the code by hand, on paper, you would spot the problem in a few minutes.

    However, you can simplify the code by re-arranging it like this:
    ...
    amount = Integer.parseInt(input);
    remainingAmount = amount % 100;
    hundreds = (amount - remainingAmount) / 100;
     
    amount = remainingAmount;
    remainingAmount = amount % 50;
    fifties = (amount - remainingAmount) / 50;
     
    amount = remainingAmount;
    remainingAmount = amount % 20;
    twenties = (amount - remainingAmount) / 20;
    ...
    etc.
    Notice that doing it this way, the only difference each time is the units number, which suggests you could abstract the calculation into a single method, making things even simpler. When you learn about the 'static' keyword, you could do something like this:
    static int remainingAmount;
    static int amount;
    ...
    {
       ...
       hundreds = getUnits(Integer.parseInt(input), 100);
       fifties      = getUnits(remainingAmount, 50);
       twenties = getUnits(remainingAmount, 20);
       tens       = getUnits(remainingAmount, 10);
       fives       = getUnits(remainingAmount, 5);
       twos      = getUnits(remainingAmount, 2);
       ...
    }
     
    // calculate the units
    static int getUnits(int anAmount, int unitValue) {
        amount = anAmount;
        remainingAmount = amount % unitValue;
        return (amount - remainingAmount)/unitValue;
    }