Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Tolls

Thread: euro converter to dollars

  1. #1
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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(); 
    		}
    		}
    			}:^):

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,858
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  4. #4
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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();
    		}
    		}
    			
            }

  5. #5
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  6. #6
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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;
    		}
    		}
    			
            }

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,858
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default 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.
    KevinWorkman likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default 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.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    JMAsterson is offline Member
    Join Date
    Mar 2012
    Posts
    55
    Rep Power
    0

    Default Re: euro converter to dollars

    Oh, I got it. I changed it to input.nextDouble and it worked fine! thanks a lot!

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default Re: euro converter to dollars

    Quote Originally Posted by JMAsterson View Post
    Oh, I got it. I changed it to input.nextDouble and it worked fine! thanks a lot!
    See? Reading the API documentation actually does help.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Error euro sign on oracle database
    By ct2marer in forum JDBC
    Replies: 0
    Last Post: 10-26-2009, 12:07 PM
  2. I need some help with converter GUI
    By linux1man in forum AWT / Swing
    Replies: 5
    Last Post: 07-29-2009, 11:57 PM
  3. Currency Converter!!!!
    By Pascal Nouma in forum AWT / Swing
    Replies: 2
    Last Post: 04-02-2009, 07:23 AM
  4. sound converter WAV to AU
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 03-14-2009, 10:25 AM
  5. YaHP Converter 1.2.12
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-12-2007, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •