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.

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.

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.

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.

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.

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.

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.

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)

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){

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;
		
	}

what errors?
See my last post re =

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.

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.

I'm not sure why mine doesn't do that, I'm running eclipse. That is what came up in the console. Here is a picture of what I am talking about:

http://tinypic.com/view.php?pic=34pausn&s=5

Maybe that will help?

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.

........... 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.

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.

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.

Is there a question there or is it working now?

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;
	}

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){

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.

Ok, good luck.

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;
		}
	}

}

I am having trouble getting my program to write to a file.

Please explain.

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.

Did that explain it better?

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.

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());
	}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.