Click to See Complete Forum and Search --> : Creating a program should print equivalent amount
A. Al Kaabi
11-03-2006, 03:21 PM
Hi everyone,
Actually I am a beginner in using Java program. And I would ask a question.
The user should input an amount of money and it should be double, the output should be : # tens bills, # five bills, # ones coins, # fifty (half a $)coins...
The Question is how could I creat this program that I should use the money entered by the user as an integer,, and after that use it as a double to imput the last value,, and all the output should be in the same dialog box???
agent_x91
11-06-2006, 08:05 AM
I'm confused by your question... I understand you want the input for the amount of different bills, but what is it you plan to do with it exactly? :confused:
A. Al Kaabi
11-07-2006, 12:12 PM
Thanks for replying,
Well, the question said that I should ask the user to enter a "DOUBLE" value representing this amount of money. If For example they entered $23.80 the output will be as the following:
2 ten bills
0 five bills
3 one (coins)
1 (half a dirham;UAE currency)
1(25 fils)
0 (ten fils)
1 (five fils)
--------
But if you recognize the last 4 output they should be double values,, and I used the value as an integer to output the first 3 outputs. My problem is if I put the "MONEY value" as an integer the last 4 output doesnt show, and if I put is as a double ,, all the value will be in the first output which is "The ten bills"
***THe Q is how could I used the money value that the user enterd to have the same out put as above in the java program.
Thanks,
Miss Al Kaabi
Perhaps if would help if you post the code that you have already done?
Personally I would use charAt() and modulus (%) to split the number apart.
A. Al Kaabi
11-07-2006, 01:54 PM
This is the code I have done!
import javax.swing.JOptionPane;
public class PartA {
public static void main(String[] args) {
String money_s;
double money, fifty_f, twenty_f, ten_f, five_f,one_f;
int money_int, tens, fives, ones;
String Message="";
money_s=JOptionPane.showInputDialog("Enter an amount of money : ");
money_int=Integer.parseInt(money_s);
tens=(money_int/ 10);
fives=(money_int-(tens*10))/5;
ones=(money_int-((tens*10)+(fives*5)))/1;
money=Double.parseDouble(money_s);
fifty_f=(money-((tens*10)+(fives*5)+(ones)));
Message+= tens+" ten dirham bills \n"+fives+" five dirham bills \n" +ones+" one dirham coins\n"+fifty_f+ " fifty fils (half a dirham) coins";
JOptionPane.showMessageDialog(null, Message, "Information", JOptionPane.INFORMATION_MESSAGE);
Try this:
import javax.swing.JOptionPane;
public class partA {
public static void main(String[] args) {
String money_s;
int money_int, tens, fives, ones, stringLength, money;
int fifty_f;
String Message="";
money_s=JOptionPane.showInputDialog("Enter an amount of money: ");
//Find period as everything before is used to calculate 10s, 5s and 1s.
int firstPeriod = money_s.indexOf (".");
String dollar = money_s.substring (0,firstPeriod);
//assign number before period to money_int
money_int = Integer.parseInt(dollar);
tens=(money_int/ 10);
System.out.println("Tens: " + tens);
fives=(money_int-(tens*10))/5;
System.out.println("Fives: " + fives);
ones=(money_int-((tens*10)+(fives*5)))/1;
System.out.println("Ones: " + ones);
//As we know where the period occus (firstPeriod variable) we know that
//everything after this point is a portion of a dollar
//Get string length
stringLength = money_s.length();
//Create string for everything after period (+1 moves us 1 character back from the period)
String cents = money_s.substring(firstPeriod+1,stringLength);
//Assign cents to money variable
money=Integer.parseInt(cents);
fifty_f=(money/50);
System.out.println("Fifty: " + fifty_f);
Message+= "The amount of " + money_s + " contains:\n" +tens+" ten dirham bills \n"+fives+" five dirham bills \n" +ones+" one dirham coins\n"+fifty_f+ " fifty fils (half a dirham) coins";
JOptionPane.showMessageDialog(null, Message, "Information", JOptionPane.INFORMATION_MESSAGE);
}
}
A. Al Kaabi
11-07-2006, 03:38 PM
Thanks "Sae".. But Unfortunately it contains the same mistake ,, than I can not input a double number as the question said ,, But thanks any way :)
Miss Al Kaabi
What do you mean by input a double number? I can add many double numbers such as 123.45, etc.
What do you mean by this:
I should use the money entered by the user as an integer,, and after that use it as a double to imput the last value,,
What is the "last value"? :confused:
A. Al Kaabi
11-08-2006, 01:10 AM
Hi,,
You know when I entered a double number , the program said "Process completed" but I cant see the Message Dialog box, that shows the output??? ANy Idea!!
and I was thinking that it wasn't working..
Miss Al Kaabi
Sorry. I don't know what you mean. When I run the program I get a box asking to input my number and then another box that tells me the answers.
A. Al Kaabi
11-08-2006, 12:55 PM
Sorry then ,, there is something wrong with my program..
Thanks any way for your help:)
A. Al Kaabi
11-08-2006, 01:02 PM
Thanks Bro It just worked,, =),, I copy i t and used another project and it finally works,, Thank you so much..=)
Miss Al Kaabi
agent_x91
11-13-2006, 03:46 AM
I think I've seen about 6 people ask for this same program over a couple of years. Maybe we should remember this thread if anyone wants help with this program in future :cool: