Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 06-13-2007, 01:06 AM   #1
Beta Member
 
Join Date: Jun 2007
Posts: 4
Default JAVA Currency Exch. Program - IO(InputMismatchException) HELP!

I am new to Java and could use some help with my program. The program has two classes, accepts the inputs of commission percentage, exchange rates, and amount values (via JOptionPane windows), and outputs to a file and to a JOptionPane window.

I ran the program and received:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextDouble(Scanner.java:2335)
at Banking.readRates(Banking.java:99)
at BankingApplication.main (BankingApplication.java:147)

I would really appreciate it if someone could explain why I am receiving this error and how to fix it. My code is below (The second class - BankingApplication - contains the main method)

The input file contains the following data:

Code:
2.1
0.9
0.77
0.11
0.095
1.4
1.29
Code:
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;

public class Banking {

  private static double commissionPerCent; 
  private static double buyingRateC; 
  private static double sellingRateC; 
  private static double buyingRateM; 
  private static double sellingRateM; 
  private static double buyingRateE; 
  private static double sellingRateE; 
  private double amount;
  private char code;
  private boolean buy;
  private String report = "";
  private static DecimalFormat df = new DecimalFormat("0.00"); 
  
  public void readRates() throws
                             FileNotFoundException  //Throws an IOExeption.
  {
    Scanner inFile = new Scanner("a:\\dailyrates.txt"); 

    commissionPerCent = inFile.nextDouble();
    buyingRateC = inFile.nextDouble();
    sellingRateC = inFile.nextDouble();
    buyingRateM = inFile.nextDouble();
    sellingRateM = inFile.nextDouble();
    buyingRateE = inFile.nextDouble();
    sellingRateE = inFile.nextDouble();

    inFile.close();

  }//end method readRates

  
  public double priceInDollars(double rate, double amount, boolean flag)  {

    double dollarValue;
    double commission;
    double dollarCost;

    dollarValue = amount * rate;
    commission = dollarValue * (commissionPerCent/100);

    if (flag = true)
    {
      dollarCost = dollarValue + commission;
    }
    else
      dollarCost = dollarValue - commission;

    return dollarCost;
  }//end method priceInDollars

  
  public double composeReport()  {

    double rate = 0;

    switch (code)
    {
      case 'c':
      case 'C':
        if (buy)
        {
          rate = buyingRateC;
          report = report + "buys  " + df.format(amount) + " CD ";
        }
        else
        {
          rate = sellingRateC;
          report = report + "sells  "+ df.format(amount) + " CD ";
        }
        break;
      case 'm':
      case 'M':
        if (buy)
        {
          rate = buyingRateM;
          report = report + "buys  "+ df.format(amount) + " MP ";
        }
        else
        {
          rate = sellingRateM;
          report = report + "sells  "+ df.format(amount) + " MP ";
        }
        break;
      case 'e':
      case 'E':
        if (buy)
        {
          rate = buyingRateE;
          report = report + "buys  "+ df.format(amount) + " EU ";
        }
        else
        {
          rate = sellingRateE;
          report = report + "sells  "+ df.format(amount) + " EU ";
        }
        break;
      default: System.out.println("Invalid currency.");
    }

    double dollarPrice = priceInDollars(rate, amount, buy);

    return dollarPrice;

  }//end method composeReport

  
  // The method displayResult calls the method composeReport(), composes the final output message,
  // opens the message window, and writes the report to the output file.
  
  public void displayResult() throws
                                IOException
  {
    FileWriter file = new FileWriter("a:\\dailyreport.txt", true);
    PrintWriter outFile = new PrintWriter(file);

    outFile.printf("Customer " +report +" for " + composeReport() +"  dollars. \n");
    outFile.close();

    if (buy = true)
    {
      JOptionPane.showMessageDialog(null, "Your charge for the transaction is: \n"
                                    + composeReport() + " dollars. \n"
                                    + "We appreciate your business!", "Payment",
                                    + JOptionPane.INFORMATION_MESSAGE);
    }
    else
    {
      JOptionPane.showMessageDialog(null, "You receive \n" +composeReport() +" dollars. \n"
                                    +"We appreciate your business!", "Payment",
                                    + JOptionPane.INFORMATION_MESSAGE);
    }

  }//end method displayResult

  
  // Banking() is the default constructor.
  
  public Banking() {
  }//end default constructor Banking

  
  // The constructor Banking takes three parameters: double amount for the curency amount, char code
  // for the code and char bs showing the buy/sell choice; amount and code are assigned to the
  // corresponding class data; true is assigned to buy if bs is 'b' or 'B', and false
  // is assigned otherwise.
  
  public Banking(double amount, char code, char bs)  {

    this.amount = amount;
    this.code = code;

    if (bs == 'b')
    {
      buy = true;
    }
    else if (bs == 'B')
    {
      buy = true;
    }
    else
    {
      buy = false;
    }

  }//end constructor Banking
}//end class Banking
Code:
import javax.swing.*;
import java.io.*;

public class BankingApplication {

  //
  // The method inputCurrency obtains and returns the currency code from the user.
  //
  public static char inputCurrency() {

    String currencyString;

    currencyString = JOptionPane.showInputDialog(null, "We exchange \nCD (Canadian Dollar)"
                                                 +"\nMP (Mexican Peso) \n EU (Euro) \n Enter "
                                                 +"the currency code:", "Welcome to Modest Internation",
                                                 JOptionPane.QUESTION_MESSAGE);

       if (currencyString == null || currencyString.equals("")) {
      JOptionPane.showMessageDialog(null, "Goodbye!", "Modest International",
                                    JOptionPane.INFORMATION_MESSAGE);
      System.exit(0);
      }

    return currencyString.charAt(0); //Return the first character of currencyString.
  }//end method inputCurrency

  
  // The method inputAmount obtains and returns the currency amount from the user.
  
  public static double inputAmount()  {

    String amountString; 

    amountString = JOptionPane.showInputDialog(null, "Please enter amount:", "Modest International",
                                               JOptionPane.QUESTION_MESSAGE);

       if (amountString == null || amountString.equals(""))
    {
      JOptionPane.showMessageDialog(null, "Goodbye!", "Modest International",
                                    JOptionPane.INFORMATION_MESSAGE);
      System.exit(0);
    }

    return Double.parseDouble(amountString); //Return the value entered as type double.
  }//end method inputAmount

  
  // The method buyOrSell obtains the buy/sell option from the user.
  
  public static char buyOrSell()  {

    String buyOrSellString; 

    buyOrSellString = 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.",
                                                 "Modest Internation", JOptionPane.QUESTION_MESSAGE);

    if (buyOrSellString == null || buyOrSellString.equals(""))
    {
      JOptionPane.showMessageDialog(null, "Goodbye!", "Modest International",
                                    JOptionPane.INFORMATION_MESSAGE);
      System.exit(0);
    }

    return buyOrSellString.charAt(0); //Return the first character of buyOrSellString.
  }//end method buyOrSell

  
  // The method newTransaction determines whether or not the user wants a new transaction.
  
  public static int newTransaction()  {

    return JOptionPane.showConfirmDialog(null, "Would you like to make another transaction",
                                         "Modest International", JOptionPane.YES_NO_OPTION,
                                         JOptionPane.QUESTION_MESSAGE);
  }//end method newTransaction

  
  // The method main instantiates a Banking class object with the actual parameters being the return
  // values obtained by the input windows; calls displayResult and readRates with respect to bank;
  // runs a while loop until newTransaction() returns NO.
  
  public static void main(String[] args) throws
                                           IOException
  {
    inputCurrency(); 
    inputAmount(); 
    buyOrSell(); 

    Banking bank = new Banking(inputAmount(), inputCurrency(), buyOrSell());

    bank.readRates();
    bank.displayResult();
    
    newTransaction();
    if (newTransaction() == JOptionPane.NO_OPTION)
      System.exit(0);
    while (newTransaction() == JOptionPane.YES_OPTION)
    {
      inputCurrency();
      inputAmount();
      buyOrSell();

      Banking bank1 = new Banking(inputAmount(), inputCurrency(), buyOrSell());
      bank1 = bank;
      bank.displayResult();
    }

  }//end method main
}//end class BankingApplication
__________________

magic507 is offline   Reply With Quote
Old 06-20-2007, 02:13 PM   #2
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: JAVA Currency Exch. Program - IO(InputMismatchException) HELP!

well, without compiling it an running it, I would assume that your problem comes because you are using the scanner object to load the values from your text file, but you aren't parsing them when you load them. You might try for your readRates method:
Code:
    Scanner inFile = new Scanner("a:\\dailyrates.txt"); 
    try {
    commissionPerCent = inFile.nextDouble();
    buyingRateC = double.praseDouble(inFile.next());
    sellingRateC = double.praseDouble(inFile.next());
    buyingRateM = double.praseDouble(inFile.next());
    sellingRateM = double.praseDouble(inFile.next());
    buyingRateE = double.praseDouble(inFile.next());  
    sellingRateE = double.praseDouble(inFile.next());
    } catch (NumberFormatException NFE) {
       System.err.println(NFE.toString() + "\n");
       NFE.printStackTrace();
    } catch (Exception e) {
       System.err.println(e.toString() + "\n");
       e.printStackTrace();
    }
    inFile.close();
That isn't compiled code, so you might have to tweak it
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 05:44 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0