Scanf Equivalent

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Scanf Equivalent

 
0
  #1
Jan 16th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Scanf Equivalent

 
0
  #2
Jan 16th, 2005
What's the scanf function? I'm not familar with C++..
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Re: Scanf Equivalent

 
0
  #3
Jan 16th, 2005
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]);
		
		
	     }
	}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Scanf Equivalent

 
0
  #4
Jan 16th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Scanf Equivalent

 
0
  #5
Jan 16th, 2005
instead of this:

public static void main(String[] args)


do this:

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


then it will compile
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Re: Scanf Equivalent

 
0
  #6
Jan 16th, 2005
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 }.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Scanf Equivalent

 
0
  #7
Jan 16th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Re: Scanf Equivalent

 
0
  #8
Jan 17th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Scanf Equivalent

 
0
  #9
Jan 17th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: AQWst is an unknown quantity at this point 
Solved Threads: 0
AQWst AQWst is offline Offline
Light Poster

Re: Scanf Equivalent

 
0
  #10
Jan 17th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC