I am new to Java programming and am converting a program I wrote in C++ to Java. It is a currency conversion program that uses a number of functions to display different menus. It also allows for user input of the menu options to either do a currency conversion or transfer to a different menu which is in a different function.
My question is, I am just getting started and can not find a Java equivalent to C++'s scanf command. I want to use it to read in a string array (for the menu option). My menu option consists of a single letter and a two digit number. By having the menu option defined as an array, I can convert the first letter portion to upper case if the user accidentally enters it in lower case. Then, the number portion can be validated as being a range of valid numbers.
Once I have selected the appropriate menu option to do a conversion, I ask the user to enter a currency amount, defined as a double to be converted to the new currency. Is the same operation code that is used for the menu option able to support this currency amount, or is a different operation code necessary for this?

Recommended Answers

All 14 Replies

What's the scanf function? I'm not familar with C++..

Is is an operation code that allows user input into a program. I am also including what code I have entered so far and will bold the line that I am getting the error message "unreported exception java.io.IOEXCEPTION; must be caught or declared to be thrown"

import java.io.*;                 // this one is for the system.io 
	public class CurrencyConv
	{
        public static void main (String[] arguments) 
	     { 
	     String Menu_Opt[] = {"    "};    // Menu Option 
	     int      Counter;		      // For Loop Positioner	
	     BufferedReader inStr = new BufferedReader(new InputStreamReader(System.in));
	     double   Org_Curr_Amt;           // Original Currency Amount    
	     double   New_Curr_Amt;           // New Currency Amount         
	     String  Currency_Desc[] = { "American Dollar (USD)", "Australian Dollar (AUD)",
	       "Brazilian Real (BRL)", "British Pound (GBP)", "Canadian Dollar (CAD)",
	       "Chinese Yuan (CNY)", "Danish Krone (DKK)", "Euro (EUR)", "Hong Kong Dollar (HKD)", 
	       "Indian Rupee (INR)", "Japanese Yen (JPY)", "Malaysian Ringgit (MYR)", "Mexican Peso (MXN)",
	       "New Zealand Dollar (NZD)", "Norwegian Kroner (NOK)", "Singapore Dollar (SGD)", 
	       "South African Rand (ZAR)", "South Korean Won (KRW)", "Sri Lanka Rupee (LKR)", 
	       "Swedish Krona (SEK)", "Swiss Franc (CHF)", "Taiwan Dollar (TVD)", "Thai Baht (THB)",
	       "Venezuelan Bolivar (YEB)" };  // Descriptions of each of the currency types that can be converted
	     double USD_to_AUD = 1.28436;     // Australian Dollar (AUD)     
	     double USD_to_BRL = 2.7675;      // Brazilian Real (BRL)        
	     double USD_to_GBP = 0.540132;    // British Pound (GBP)         
	     double USD_to_CAD = 1.207;       // Canadian Dollar (CAD)       
	     double USD_to_CNY = 8.2765;      // Chinese Yuan (CNY)          
	     double USD_to_DKK = 5.7225;      // Danish Krone (DKK)          
	     double USD_to_EUR = 0.770297;    // Euro (EUR)                  
	     double USD_to_HKD = 7.7767;      // Hong Kong Dollar (HKD)      
	     int    USD_to_INR = 45;          // Indian Rupee (INR)          
	     double USD_to_JPY = 104.23;      // Japanese Yen (JPY)          
	     double USD_to_MYR = 3.8;         // Malaysian Ringgit (MYR)      
	     double USD_to_MXN = 11.342;      // Mexican Peso (MXN)           
	     double USD_to_NZD = 1.41123;     // New Zealand Dollar (NZD)    
	     double USD_to_NOK = 6.2644;      // Norwegian Kroner (NOK)      
	     double USD_to_SGD = 1.6538;      // Singapore Dollar (SGD)      
	     double USD_to_ZAR = 6.0284;      // South African Rand (ZAR)    
	     int    USD_to_KRW = 1069;        // South Korean Won (KRW)      
	     double USD_to_LKR = 104.88;      // Sri Lanka Rupee (LKR)       
	     double USD_to_SEK = 6.9108;      // Swedish Krona (SEK)         
	     double USD_to_CHF = 1.1691;      // Swiss Franc (CHF)           
	     double USD_to_TVD = 32.635;      // Taiwan Dollar (TVD)         
	     double USD_to_THB = 40.16;       // Thai Baht (THB)             
	     double USD_to_YEB = 1915.2;       // Venezuelan Bolivar (YEB)    
	
		System.out.println("Currency Conversion"); 
		System.out.println("United States Dollars Conversion Menu\n");
		System.out.println("");
		System.out.println("A02.  USD to AUD        Y02.  Transfer to AUD Conversion Menu");
		System.out.println("A03.  USD to BRL        Y03.  Transfer to BRL Conversion Menu");
		System.out.println("A04.  USD to GBP        Y04.  Transfer to GBP Conversion Menu");
		System.out.println("A05.  USD to CAD        Y05.  Transfer to CAD Conversion Menu");
		System.out.println("A06.  USD to CNY        Y06.  Transfer to CNY Conversion Menu");
		System.out.println("A07.  USD to DKK        Y07.  Transfer to DKK Conversion Menu");
		System.out.println("A08.  USD to EUR        Y08.  Transfer to EUR Conversion Menu");
		System.out.println("A09.  USD to HKD        Y09.  Transfer to HKD Conversion Menu");
		System.out.println("A10.  USD to INR        Y10.  Transfer to INR Conversion Menu");
		System.out.println("A11.  USD to JPY        Y11.  Transfer to JPY Conversion Menu");
		System.out.println("A12.  USD to MYR        Y12.  Transfer to MYR Conversion Menu");
		System.out.println("A13.  USD to MXN        Y13.  Transfer to MXN Conversion Menu");
		System.out.println("A14.  USD to NZD        Y14.  Transfer to NZD Conversion Menu");
		System.out.println("A15.  USD to NOK        Y15.  Transfer to NOK Conversion Menu");
		System.out.println("A16.  USD to SGD        Y16.  Transfer to SGD Conversion Menu");
		System.out.println("A17.  USD to ZAR        Y17.  Transfer to ZAR Conversion Menu");
		System.out.println("A18.  USD to KRW        Y18.  Transfer to KRW Conversion Menu");
		System.out.println("A19.  USD to LKR        Y19.  Transfer to LKR Conversion Menu");
		System.out.println("A20.  USD to SEK        Y20.  Transfer to SEK Conversion Menu");
		System.out.println("A21.  USD to CHF        Y21.  Transfer to CHF Conversion Menu");
		System.out.println("A22.  USD to TVD        Y22.  Transfer to TVD Conversion Menu");
		System.out.println("A23.  USD to THB        Y23.  Transfer to THB Conversion Menu");
		System.out.println("A24.  USD to YEB        Y24.  Transfer to YEB Conversion Menu\n");
		System.out.println("Y99.  Exit Program");
		System.out.println("Option: "); 
		
		for (Counter = 0; Counter <= 3; Counter++)  
             		[B]Menu_Opt[Counter] = inStr.readLine[U]([/U]);[/B]
				
		//Menu_Opt = MyInput.readString();		
		System.out.println(Menu_Opt[0]);
		Menu_Opt[0] = Menu_Opt[0].toUpperCase();
		System.out.println(Menu_Opt[0]);
		System.out.println(Currency_Desc[0]);
		
		
	     }
	}

>I am just getting started and can not find a Java equivalent to C++'s scanf command.
You shouldn't be using scanf in C++. The best way to parse input remains the same for C++ and Java: read a line and then tokenize it.

instead of this:

public static void main(String[] args)


do this:

public static void main(String[] args) throws IOException


then it will compile

I'm getting a clean compile, but when I try to enter a menu option, like a01, I get the error, "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at CurrencyConv.main(CurrencyConv.java:92)" I have Menu_Opt defined as follows (at the top of the program): String Menu_Opt[] = { }; (where there are four blanks between the { and }.

>String Menu_Opt[] = {" "};
What makes you think that Menu_Opt has more than index 0? The size is derived from the initialization list, which contains exactly one item.

Are you saying that I shouldn't have Menu_Opt defined as a string array in order to separate the first character of the string and the last two numbers as separate array elements? Or, if that isn't the case, I don't understand what you mean. Shouldn't I start my array elements starting at number 0? I thought that Java was like C++ that the first element is referred to element 0.

It does start at element or index 0. You only have one element in your array or [0]. You have a for loop trying to shove mulltiple things in an array with only one element. I'm not sure I understand what your wanting to do with the menu_Opt array.

I want to split up the string called Menu_Opt into two separate parts. An example of a possible value for this is a02. The first part of the menu option will indicate a reference to my first menu. The second part, 02, refers to the second menu option on this menu. I want to have these as two separate parts to be able to validate each one separately and if there is an error with either one, I want to let the user know and exit the program. Also, if the user happens to enter a lower case letter for the first part, I want to be able to convert it to upper case without causing an error condition. I hope that this makes sense.

Maybe I can use the Menu_Opt as just a solid string value, then split it into each of the two separate parts or two separate variables. Does Java support a substring function or something similar to split this Menu_Opt field into two separate parts?

have you ever looked at the API docs for Java?
There's a very comprehensive entry on the String class, which contains quite a few handy functions including substring, split, indexing functions, etc. etc. etc.
Then there's a lot of other classes that provide String handling and manipulation functionality.

Maybe if you spent more time studying and less time waiting for answers to your questions (you can even combine the two activities quite well) you'd figure such things out more quickly.

The API docs can be downloaded from Sun, where you'll also find the Java Tutorial which, though not perfect, is rather good.

Next I'd recommend you purchase and work your way through a book: Head First Java by Kathy Sierra and Bert Bates, which is published by O'Reilly.

Hi everyone,

Reading in from a console screen involves streams. see the below link for more details

http://leepoint.net/notes-java/examples-introductory/console/console-input-output.html

If you are doing currency exchange it would be great if it was using guis' instead of in console. If want to learn the basic's of gui go to sun's official website and download the awt and swing tutorials to help you get started.

Have a wonderfull time programming in Java

Yours Sincerely

Richard West

I am now getting the string Menu_Opt to be read in properly and show up properly when I output it back to the screen. The trouble is when I try to do a substring to split it up into the different parts that I want to work with, I am not getting the results I want. I have some extra System.out.println statements to see what values I have for each of my string variables and am getting blanks for the substring created variables (Menu_ID, and Opt_Num).FYI, I have been searching the Sun web site for API's and also running Google searches for any programming help, but haven't been able to find what I need...here is my code for this section of the program...Also, is there a special op code for reading in numeric data?

Menu_Opt = inStr.readLine();
		Menu_ID = Menu_Opt.substring(1, 1);
		Opt_Num = Menu_Opt.substring(2, 2);
		System.out.println(Menu_Opt);
		System.out.println(Menu_ID);
		System.out.println(Opt_Num);
		Menu_Opt = Menu_Opt.toUpperCase();
		System.out.println(Menu_Opt);
		System.out.println(Currency_Desc[0]);

remember using the substring function that it also starts at index 0. So if you wanting to get the first character, I think you would do this:

menu_opt.substring(0,1);

and the second one :

menu_opt.substring(1,2);

As for the numeric data, I think it depends on your jdk version. In some you may be able to do this:

indata.readDouble();

some not.

You can always take the inputed string and convert it to a Double or int

int x = Integer.parseInt(string);

or

double d = Double.doubleValue(String);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.