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 :o
This is my code so far: My "Output" isn't complete, and don't currently need help there.
Code :
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 :confused:
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 =D>)
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:
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:
Code :
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"
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
Code :
String fivesAsString = Integer.toString(fives);
System.out.println("fives + (" + fivesAsString + )");
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).
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.
Code :
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.
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.
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 ~X(
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 :confused:
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
Code :
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) + ")");
}
}
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:
Code :
...
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:
Code :
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;
}