Results 1 to 13 of 13
Thread: euro converter to dollars
- 04-03-2012, 09:36 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
euro converter to dollars
I'm writing a program that repeatedly performs currency conversion from euros to dollars: it prompts the user for the conversion factor (i.e. the number that you use to multiply the number of euros to get the number of dollars). The program then repeatedly asks the user for a quantity to convert, considers each quantity to be an amount in euros and displays the equivalent number of dollars. When the user enters 0 or a negative amount, the program quits.
I'm having a few issues. First it wont compile.
I'm getting an error on " while (Quantity <= 0) " the operator is undefined. Also an error the operator * is undefined. I can see why im getting these errors. But im not sure how else to write this program in order to do what I want. Please take a look at my program and let me know if you see something i should fix - or if something else would work better in place of parts of what I have. Thanks!
Here is my code!
Java Code:import java.util.Scanner; public class EuroConverter { public static void main(String[] args) { Scanner in = new Scanner(System.in); String Converter; String Quantity; int total; System.out.println("Enter a conversion factor."); Converter = in.next(); System.out.println("Enter a quantity to convert from Euro's to dollars or enter a quantity less than or equal to 0 to exit the program.."); total = (int) (Converter * Quantity); while (Quantity <= 0) { System.out.println("The quantity of dollars is:" + Quantity); System.out.println("Enter a word or type stop to quit the program."); Quantity = in.next(); } } }:^):
- 04-03-2012, 09:39 PM #2
Re: euro converter to dollars
Just a word to the wise- you're asking a lot of questions that would be more easily answered by a google search. Especially because you seem to be ignoring advice (using in.nextLine() instead of in.next() for example).
That being said, what variable is involved in the message? What type is that variable (int, boolean, something else)? Do the operators you're talking about apply to that type?How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-03-2012, 09:51 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: euro converter to dollars
I did use nextLine instead odin.next once it was suggested. I carefully look at everyones responses and try very hard to understand what they're saying. Almost always its very difficult to find specifically what i'm looking for on google. I really appreciate the help you and every one else if giving (and dont worry, this is my last question for a little while). Thanks for taking the time to help.
So i was trying to use int. Well, this "total = (int) (Converter * Quantity);" for example, I understand is not working - but i dont know how else to write that to work with my program.
I am using strings also.
- 04-04-2012, 12:07 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: euro converter to dollars
Ok, so I totally rearranged my program.
It almost works perfectly, except that the program doesnt quit when the number is less than or equal to 0. (Which it should.) It just doesn't give a total.
Here is the code:
Java Code:import java.util.*; public class Euro { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a conversion factor. "); int num1 = input.nextInt(); System.out.print("Please enter a quantity or enter a 0 or less to stop the program."); int num2 = input.nextInt(); int total = num1 * num2; while (num2 >= 0) { System.out.println("The quantity of dollars is:" + total); System.out.print("Please enter a quantity or enter a 0 or less to stop the program."); num2 = input.nextInt(); } } }
- 04-04-2012, 02:09 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: euro converter to dollars
Got it, changed it to (num2 > 0) and it worked. Your first reply helped me realize i needed to make sure i had my program structured correctly. Thanks a lot.
- 04-05-2012, 02:04 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: euro converter to dollars
If anyone sees this, i could use some advice on this post as to how to make my finished program accept decimals. It will get an error if i put in an integer. I changed the int's to double. But i still cant figure out how to make the conversion factor accept decimals. Thanks for the help!
Java Code:import java.util.*; public class Euro { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a conversion factor. "); double num1 = input.nextInt(); System.out.print("Please enter a quantity or enter a 0 or less to stop the program."); double num2 = input.nextInt(); double total = num1 * num2; while (num2 > 0) { System.out.println("The quantity of dollars is:" + total); System.out.print("Please enter a quantity or enter a 0 or less to stop the program."); num2 = input.nextInt(); total = num1 * num2; } } }
- 04-05-2012, 06:10 AM #7
Re: euro converter to dollars
You've already been advised to check out the Scanner API. Have you done so?
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-05-2012, 05:21 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: euro converter to dollars
Yes, which does have a lot of useful information. (That is, if this is the site youre referring too.) Scanner (Java Platform SE 6)
The site seemed to have information on nextBigDecimal and things like that, but not (at least that i could find), information to help my problem specifically. it seemed like a lot of the information was general so i wasnt sure which information to apply to my problem.
- 04-05-2012, 05:44 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: euro converter to dollars
Show the code that fails to accept decimals.
If it's the above code then why do you think nextInt() is a good choice for accepting decimal numbers.
Also, if you get exceptions it's a good idea to post them here, indicating the line on which they occur.
This helps us to see the actual problem, as well as allows us to explain what the exception means.
Translating and understanding exceptions is an important part of this whole coding lark.Please do not ask for code as refusal often offends.
** This space for rent **
- 04-05-2012, 08:12 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: euro converter to dollars
Here is an example of what happens when i put in a decimal for the conversion factor, and the error i get.
Please enter a conversion factor. .884
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Euro.main(Euro.java:9)
I can see that there is an error with nextInt, but i'm not sure what to change it to as nextLine doesnt work or simply input.next(); doesnt work either.
- 04-05-2012, 08:22 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: euro converter to dollars
Persenting .884 scares the hell out of the poor nextInt() method because it can only accept ints. Read the API documentation for the Scanner class again and see if there is a method that can read representations of double numbers.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 04-05-2012, 08:25 PM #12
Member
- Join Date
- Mar 2012
- Posts
- 55
- Rep Power
- 0
Re: euro converter to dollars
Oh, I got it. I changed it to input.nextDouble and it worked fine! thanks a lot!
- 04-05-2012, 08:36 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Similar Threads
-
Error euro sign on oracle database
By ct2marer in forum JDBCReplies: 0Last Post: 10-26-2009, 01:07 PM -
I need some help with converter GUI
By linux1man in forum AWT / SwingReplies: 5Last Post: 07-30-2009, 12:57 AM -
Currency Converter!!!!
By Pascal Nouma in forum AWT / SwingReplies: 2Last Post: 04-02-2009, 08:23 AM -
sound converter WAV to AU
By Unome in forum Java AppletsReplies: 5Last Post: 03-14-2009, 11:25 AM -
YaHP Converter 1.2.12
By levent in forum Java SoftwareReplies: 0Last Post: 06-12-2007, 09:49 AM
Bookmarks