| | |
Scanf Equivalent
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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++)
Menu_Opt[Counter] = inStr.readLine();
//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]);
}
}•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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 }.
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Jan 2005
Posts: 31
Reputation:
Solved Threads: 0
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.
![]() |
Similar Threads
- Tic-Tac-Toe (C++)
Other Threads in the Java Forum
- Previous Thread: Need some help with Round Robin scheduling
- Next Thread: How do I Take in Hexidecimal input out put binary, decimal, and octal
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows






