0

Do more debuggging by printout the values of all 3 variables that are passed to the priceInDollars() method to see if they are all valid. If they are valid, the go into the priceInDollars() method and print out the values of all the computations it does so you can see if the formulas are correct.

0

Ok so here is the output for my test code:

input currency: c
input amount: 10.0
buy or sell: false
rate: 0.0 amount: 10.0 buy false
price in dollars: 0.0
Modest International is closed for the day. See you tomorrow!

So this concludes that it is getting proper values for input amount, but is not receiving proper values for rate, but the formula is computing correct. The rates should be coming from the text file "dailyrates.txt" which is read through scanner in the readRates() method, but this method doesn't return a value, it just reads the data in the text file. How can I get a rate from this?

Text File:
COM 2.1
CDB 1.05
CDS 0.93
MPB 0.11
MPS 0.095
EUB 1.554
EUS 1.429

The numbers being the rate that I need to multiply the amount by.

0

How can I get a rate from this?

Where are you storing the rates when you read them into the program?
That is where you'd to go get the rate.

0

Ok I have not stored a rate yet, but in order to do that I would have to use a switch and if statements. I cannot properly do that yet because my code here:

public static boolean buyOrSell(){
		String buyOrSell = JOptionPane.showInputDialog(null, "Please \n type \"b\" or \"buy\" if you want to buy and" +
				" \n type \"s\" or \"sell\" if you want to sell the amount.", JOptionPane.QUESTION_MESSAGE);
		if(buyOrSell == "b")
		{
			buy = true;
		}else if(buyOrSell == "s")
		{
			buy = false;	
		}
		return buy;	
	}

Only returns false, no matter if the input is "b" or "s". If I can get this to properly execute then I can start my switch and if statements to store the rate based off of user input and what is read in the file.

0

You must use the equals() method when comparing the contents of Strings.
Only use == for comparing primitives like int and char.
Look at the code in your inputCurrency() method.

0

Ok trying to implement the switch statement to assign a value to rate. Getting alot of syntax errors. I looked at a few websites for an idea of the code, but no luck.

/*return type char; no parameter; 
	 * opens a JOptionPane input window to obtain the currency code from the user; 
	 * if the input string is null or empty, the program offers a new transaction; 
	 * otherwise returns the first character of the input string.
 	*/
	public static char inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
		
		if(choice.equalsIgnoreCase("CD"))
		{
			code = 'c';
		}else if(choice.equalsIgnoreCase("MP"))
			{
				code= 'm';
			}else if(choice.equalsIgnoreCase("EU"))
				{
					code = 'e';
				}else{
					System.out.println("Incorrect Currency Code: " + choice +"." + " Next time please choose from CD (Canadian Dollar), " +
							"MP (Mexican Peso), or EU (Euro)." +" Transaction aborted");
				}
		
		Switch (code){
			case 'C' : case 'c':
				if(buy = true){
					rate = buyingRateC;
				}
				else{
					rate = sellingRateC;
				}
				break;
			case 'M' : case 'm':
				if(buy = true){
					rate = buyingRateM;
				}
				else{ 
					rate = sellingRateM;
				}
				break;
			case 'E' : case 'e':
				if(buy = true){
					rate = buyingRateE;
				}
				else{
					rate= sellingRateE;
				}
				break;
		}
		
		return code;
	}

The red lines are appearing under Switch(code), all cases, and all if/elses.

Edited by javaNooblet: n/a

0

Getting alot of syntax errors.

You forgot to post the error messages.

You should separate the rate setting code from the get currency type code.

Edited by NormR1: n/a

0
Syntax error on tokens, delete these tokens
    Syntax error, insert ";" to complete AssertStatement
    Syntax error on token "else", { expected

    at CurrencyExchange.inputCurrency(CurrencyExchange.java:159)
    at CurrencyExchange.main(CurrencyExchange.java:40)

Edited by Nick Evan: Fixed formatting

0

Those are stranglely formatted error messages. Is that the way the compiler printed them?
The code you posted does not have 159 lines so no way I can understand that one.

What was the problem at line 40?

BTW This is an assignment statement not a test for equality:
if(buy = true){

You do not need to test if a boolean is true by doing a compare, just code:
if(buy){

Edited by NormR1: n/a

0

Ok I put it in a new method, but same errors appear:

public static double assignRate(){
		
		Switch (code){
		case 'C' : case 'c':
			if(buy = true){
				rate = buyingRateC;
			}
			else{
				rate = sellingRateC;
			}
			break;
		case 'M' : case 'm':
			if(buy = true){
				rate = buyingRateM;
			}
			else{ 
				rate = sellingRateM;
			}
			break;
		case 'E' : case 'e':
			if(buy = true){
				rate = buyingRateE;
			}
			else{
				rate= sellingRateE;
			}
			break;
	}
		return rate;
		
	}
0

what errors?
See my last post re =

Syntax error on tokens, delete these tokens
Syntax error, insert ";" to complete AssertStatement
Syntax error on token "else", { expected

at CurrencyExchange.inputCurrency(CurrencyExchange.java:159)
at CurrencyExchange.main(CurrencyExchange.java:40)

These are the errors that appear in the switch statement.

0

That is strange formatting for the error messages. I'm used to this:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

This message shows the line with the problem and what the error was on that line.

0

Can you execute a java compiler?
IDEs are wonderful sometimes and not so good other times.
This is one of the other times.

The IDE is coloring the lines that have problems. Look at them very closely and change them to make them correct.

Edited by NormR1: n/a

0

........... the "Switch" has a capital "S" which threw it all off, I changed it and no more lines. Sorry I wasted your time on that. It runs now but does not seem to be taking a value for rate still.

0

does not seem to be taking a value for rate still.

Start at where you first get the rate data and follow it to where you want to use it.
Add printlns along the way to show how its values change.

0

Ok I called the assignRate() method inside of the inputCurrency() method, now it is getting values. I think it was not because the switch statement inside of the assignRate() method relies on the "currency code" for a proper rate to be able to be assigned.

0

Yes a question, it is only getting values for the buy rate and not the sell rate. Here is the code for how it is being read in from the file:

public static void readRates() throws IOException{
		File f = new File("dailyrates.txt");	
		Scanner y = new Scanner(f);
		String com;
		String buyC;
		String sellC;
		String buyM;
		String sellM;
		String buyE;
		String sellE;
		while(y.hasNext()){
			com = y.next();
			commissionPerCent= y.nextDouble();
			buyC = y.next();
			buyingRateC= y.nextDouble();
			sellC = y.next();
			sellingRateC=y.nextDouble();
			buyM = y.next();
			buyingRateM= y.nextDouble();
			sellM = y.next();
			sellingRateM=y.nextDouble();
			buyE = y.next();
			buyingRateE= y.nextDouble();
			sellE = y.next();
			sellingRateE=y.nextDouble();
		}	
	}

And here is the code for how it is assigning the rates:

public static double assignRate(){
		switch (code){
		case 'C' : case 'c':
			if(buy = true){
				rate = buyingRateC;
			}
			else{
				rate = sellingRateC;
			}
			break;
		case 'M' : case 'm':
			if(buy = true){
				rate = buyingRateM;
			}
			else{ 
				rate = sellingRateM;
			}
			break;
		case 'E' : case 'e':
			if(buy = true){
				rate = buyingRateE;
			}
			else{
				rate= sellingRateE;
			}
			break;
		}
		return rate;
	}
0

Earlier I posted this:
The following is an assignment statement not a test for equality:
if(buy = true){

You do not need to test if a boolean is true by doing a compare, just code:
if(buy){

0

Sorry I missed that, it was the solution though. Thanks for all your help thus far! I have to go now hopefully I can figure out the rest, if not I will look for guidance from helpful people like you on the forum instead of giving me answers, actually teaching me how to do it.

0

I am having trouble getting my program to write to a file. The FileWriter is in the displayResult() method. Thanks for any help, here is my most recent code:

import java.io.*;

public class CurrencyExchange {

	final static double MIN_COMISSION = 5;
	static double commissionPercent;
	static double buyingRateC;
	static double sellingRateC;
	static double buyingRateM;
	static double sellingRateM;
	static double buyingRateE;
	static double sellingRateE;
	static double amount;
	static char code;
	static boolean buy;
	static double rate;
	static DecimalFormat df = new DecimalFormat("0.00");

	public static void main(String[] args) throws IOException {
		JOptionPane.showMessageDialog(null,"Welcome to the Modest International Currency Exchange Services!", "Modest International",1);
		readRates();
		
		System.out.println("input currency: " + inputCurrency());
		System.out.println("input amount: " + inputAmount());
		System.out.println("buy or sell: " + buyOrSell());
		System.out.println("rate: "+ rate + " amount: "+ amount + " buy "+ buy);
		System.out.println("price in dollars: " + priceInDollars(rate, amount, buy));

		while(newTransaction()== 0){
			inputCurrency();
			inputAmount();
			buyOrSell();
			displayResult();
		}
		
	}

	public static Double priceInDollars(double rate, double amount, boolean buy){
		
		double rateAmt = rate * amount;
		double baseCom = rateAmt * commissionPercent/100;
		double commission = Math.min(baseCom, 10);
		double dollarValue;
		if(buy){
			dollarValue = rateAmt += commission ;
		}else{
			dollarValue = rateAmt -= commission;
		}
		return dollarValue;
	}


	public static double composeReport(){
		priceInDollars(rate, amount, buy);
		switch (code){
		case 'C' : case 'c':
			if(buy){
				rate = buyingRateC;
			}
			else{
				rate = sellingRateC;
			}
			break;
		case 'M' : case 'm':
			if(buy){
				rate = buyingRateM;
			}
			else{ 
				rate = sellingRateM;
			}
			break;
		case 'E' : case 'e':
			if(buy){
				rate = buyingRateE;
			}
			else{
				rate= sellingRateE;
			}
			break;
		}
		
		return rate;
	}

	public static void displayResult() throws IOException{
		composeReport();
		FileWriter fw = new FileWriter("dailyreport.txt", true);
		fw.append("Customer " + buyOrSell() + " " + inputAmount() + " " + inputCurrency() + " " + priceInDollars(rate, amount, buy) + " dollars" );
		fw.close();
		if(newTransaction()==1){
			JOptionPane.showMessageDialog(null,"You receive\n" + priceInDollars(rate, amount, buy) + " dollars.\n We appreciate your business!"
					, "Payment",1);
		}else{
			JOptionPane.showMessageDialog(null,"Your charge for the transaction is: \n" + priceInDollars(rate, amount, buy) + " dollars.\n " +
					"We appreciate your business!", "Payment",1);
		}
		
	}

	public static void readRates() throws IOException{
		File f = new File("dailyrates.txt");	
		Scanner y = new Scanner(f);
		String com;
		String buyC;
		String sellC;
		String buyM;
		String sellM;
		String buyE;
		String sellE;
		while(y.hasNext()){
			com = y.next();
			commissionPercent= y.nextDouble();
			buyC = y.next();
			buyingRateC= y.nextDouble();
			sellC = y.next();
			sellingRateC=y.nextDouble();
			buyM = y.next();
			buyingRateM= y.nextDouble();
			sellM = y.next();
			sellingRateM=y.nextDouble();
			buyE = y.next();
			buyingRateE= y.nextDouble();
			sellE = y.next();
			sellingRateE=y.nextDouble();
		}	
	}

	public static char inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
				JOptionPane.QUESTION_MESSAGE);

		if(choice.equalsIgnoreCase("CD"))
		{
			code = 'c';
		}else if(choice.equalsIgnoreCase("MP"))
		{
			code= 'm';
		}else if(choice.equalsIgnoreCase("EU"))
		{
			code = 'e';
		}else{
			
			System.out.println("Incorrect Currency Code: " + choice +"." + " Next time please choose from CD (Canadian Dollar), " +
					"MP (Mexican Peso), or EU (Euro)." +" Transaction aborted");
		}
		composeReport();

		return code;
	}


	public static double inputAmount(){
		String inputAmount = JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest International", 
				JOptionPane.QUESTION_MESSAGE);
		if(inputAmount == " "){
			JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest International", 
					JOptionPane.QUESTION_MESSAGE);
		} else {
			amount = Double.parseDouble(inputAmount);
		}
		return amount;

	}

	public static boolean buyOrSell(){
		String buyOrSell = JOptionPane.showInputDialog(null, "Please \n type \"b\" or \"buy\" if you want to buy and" +
				" \n type \"s\" or \"sell\" if you want to sell the amount.", JOptionPane.QUESTION_MESSAGE);
		if(buyOrSell.equalsIgnoreCase("b"))
		{
			buy = true;
		}else if(buyOrSell.equalsIgnoreCase("s"))
		{
			buy = false;	
		}
		return buy;	
	}

	public static int newTransaction(){
		int result = JOptionPane.showConfirmDialog(null,"Would you like to make a transaction?", "Modest International",     
				JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(result == JOptionPane.YES_OPTION){
			return 0;
		}else{
			System.out.print("Modest International is closed for the day. See you tomorrow!");
			return 1;
		}
	}

}
0

Please explain.

This method:

public static void displayResult() throws IOException{
		composeReport();
		FileWriter fw = new FileWriter("dailyreport.txt", true);
		fw.append("Customer " + buyOrSell() + " " + inputAmount() + " " + inputCurrency() + " " 
				+ priceInDollars(rate, amount, buy) + " dollars" );
		fw.close();
		
		if(newTransaction()==1){
			JOptionPane.showMessageDialog(null,"You receive\n" + priceInDollars(rate, amount, buy) 
					+ " dollars.\n We appreciate your business!"
					, "Payment",1);
		}else{
			JOptionPane.showMessageDialog(null,"Your charge for the transaction is: \n" + priceInDollars(rate, amount, buy) 
					+ " dollars.\n " + "We appreciate your business!", "Payment",1);
		}
		
	}

Should write the data to the file "dailyreport.txt" but after I run the program and user enters all the data and I end the program and check the .txt file, there is no data written to it.

Edited by javaNooblet: n/a

0

I tried this for the file:

try{
		FileWriter fw = new FileWriter("dailyreport.txt", true);
		BufferedWriter out = new BufferedWriter(fw);
		out.append("Customer " + buyOrSell() + " " + inputAmount() + " " + inputCurrency() + " " 
				+ priceInDollars(rate, amount, buy) + " dollars" );
		out.close();
		}catch(Exception e){
			System.err.println("Error: " + e.getMessage());
		}

No errors print out though, but nothing gets written to the file.

0

The code works for me.
If you execute the code and there are no errors, it should write something to a file.
Here's what I executed:

try{
  		FileWriter fw = new FileWriter("TestCode7Output.txt", true);
  		BufferedWriter out = new BufferedWriter(fw);
  		out.append("This should be written to the file" );
  		out.close();
	}catch(Exception e){
		System.err.println("Error: " + e.getMessage());
	}

Edited by NormR1: n/a

0

Could it be the location of where my text file is? I have it in the src folder in eclipse.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.