Currency getCurrencyCode() Method in Java with Examples
Last Updated :
27 Dec, 2018
The getCurrencyCode() Method of Currency class in Java is used to retrieve the currency code of this currency which is actually the official ISO 4217 currency code.
Syntax:
CURRENCY.getCurrencyCode()
Parameters: This method does not accept any parameters.
Return Value: This method returns the ISO 4217 currency code of the currency.
Exceptions: The method throws Runtime Error if an invalid code is called.
Below program illustrates the working of getCurrencyCode() method:
Program 1:
import java.util.*;
public class Currency_Demo {
public static void main(String[] args)
{
Currency curr_ency
= Currency.getInstance( "INR" );
String currency_code
= curr_ency.getCurrencyCode();
System.out.println( "Currency Code of India is: "
+ currency_code);
}
}
|
Output:
Currency Code of India is: INR
Program 2:
import java.util.*;
public class Currency_Demo {
public static void main(String[] args)
{
Currency curr_ency
= Currency.getInstance( "USD" );
String currency_code
= curr_ency.getCurrencyCode();
System.out.println( "Currency Code of USA is: "
+ currency_code);
}
}
|
Output:
Currency Code of USA is: USD
Program 3: For an invalid Currency Code.
import java.util.*;
public class Currency_Demo {
public static void main(String[] args)
{
try {
Currency curr_ency
= Currency.getInstance( "USDA" );
String currency_code
= curr_ency.getCurrencyCode();
System.out.println( "Invalid Currency Code: "
+ currency_code);
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
java.lang.IllegalArgumentException
Share your thoughts in the comments
Please Login to comment...