I'm a new programming student...this is an assignment so I really need guidance or suggestions on how to make it work correctly.
I have the below code that provides conversion for a selected currency. I need to format the results with two decimal places. I think the DecimalFormat class is need somehow but I am not sure exactly where to insert the class. I tried inserting within the switch statements but keep getting an unreachable statement error (really not sure what that means).
import java.io.*; // required for BufferedReader
//gives my Java program the name Currency_wj
class Currency_wj {
//the main part of the program begins here
public static void main(String[] arguments) throws Exception {
//"throws Exception",
//throws Exception needed when reading
//from keyboard. Says you will take
//responsibility for errors.
//reads selection number from menu entered by user
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
//reads amount entered by user
BufferedReader money = new BufferedReader(
new InputStreamReader(System.in));
double ArgentinaCurrency = 2.95; //declare and initialize
double CanadaCurrency = 1.32; //declare and initialize
double calculate = 0.00;
String cname1 = "Argentina Peso"; //declare and initialize string
String cname2 = "\nCanada Dollar"; //declare and initialize string
String cnames = cname1.concat(cname2); //concatenate two Strings
String intro = "Welcome to the "; //declare and initialize String
String name = "Currency Exchange Program!"; //declare and initialize String
String title = intro.concat(name); //concatenate two Strings
double dollar; //delcare and initialize
//display combination of concatenated string
System.out.println(title + "\n\nThe currency exchange program shows\n"+
"the exchange rate of $1 American dollar\n" +
"for the following currencies:\n\n" + cnames);
char keep_going = ' '; // initialize, set initial value
String inString; //declare
char MenuChoice; //delcare
dollar = 0.00; //declare and set initial value
double amount = 0.0; //delcare and set initial value
String data = "oo"; //
while (keep_going != 'q') // will continue loop until condition met
{
//displays to screen statement in quotes
System.out.println("\nSelect a currency to convert:\n");
System.out.println(" 1. Argentina Pesos");
System.out.println(" 2. Canada Dollars");
System.out.println(" 3. Exit.");
// Send all output to the monitor and make the
// cursor visible before the user begins typing.
System.out.flush();
// "readline()" returns a String which is
// assigned to a String variable, inString
inString = br.readLine();
MenuChoice = inString.charAt(0);
// get first char of String
//switch case statement, displays data to screen depending on user selection
switch(MenuChoice)
{
case '1':
//displays data in quotes to screen
System.out.println("You want to convert Argentina Pesos");
//displays data in quotes to screen
System.out.print("Enter dollar amount to convert: ");
//displays data and displays cursor
System.out.flush();
//stores amount entered by user
data = money.readLine();
//converts string to double
amount = Double.parseDouble(data);
//displays amount entered, currency name, calculates conversion and displays results
System.out.println(amount + " Argentina Pesos equal " + amount*ArgentinaCurrency + " American Dollars");
break;
case '2':
//displays data in quotes to screen
System.out.println("You want to convert Canada Dollars");
//displays data in quotes to screen
System.out.print("Enter dollar amount to convert: ");
//displays data and displays cursor
System.out.flush();
//stores amount entered by user
data = money.readLine();
//converts string to double
amount = Double.parseDouble(data);
Include the below statements in your program just before printing the converted values. You can restrict to two decimal places using the format given by the variable myFormat.
Hope this helps.
amount = amount*ArgentinaCurrency;
String myFormat = "#.00";
DecimalFormat df = new DecimalFormat(myFormat);
String result = df.format(amount);
//displays amount entered, currency name, calculates conversion and displays results
System.out.println(amount + " Argentina Pesos equal " + result + " American Dollars");
break;
DecimalFormat df = new DecimalFormat("0.00");
and then use it:
System.out.println(amount + " Canada Dollars equal " + df.format(amount*CanadaCur