Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: InputMisMatchException

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default InputMisMatchException

    I am working on a class called Bank that holds all the accounts in an arraylist, but first reads the accounts from a file and then creates each account. I am wondering what would be the best way to fix this problem so it will read properly and create each account, below are some files and other information.

    first is the error message i get when i try to run the driver ( I already know essentially what this means i just do not know how to fix it and have the file read as well)

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextLong(Scanner.java:2196)
    at java.util.Scanner.nextLong(Scanner.java:2156)
    at Bank.load(Bank.java:81)
    at BankDriver.main(BankDriver.java:15)

    here is the file i am trying to read that contains the account information

    Billy 1000 555-1111 100.00
    Bob 1001 555-1112 101.00
    Joe 1002 555-1113 102.00
    Jonathan 1003 555-1114 103.00
    Rick 1004 555-1115 104.00


    here is the Account class
    import java.text.NumberFormat;
     
    public class Account
    {
    	private final double RATE = 0.03; // interest rate of 3.0%
     
    	private long acctNumber;
    	private double balance;
    	private String name;
    	private String phoneNumber;
     
    	//-----------------------------------------------
    	//Creates a template account
    	//-----------------------------------------------
    	public Account ()
    	{
    		name = "";
    		acctNumber = 0000;
    		phoneNumber = "555-1111";
    		balance = 0.0;
    	}
     
    	//---------------------------------------------------------------------------------------------
    	// Creates account by defining the owner, account number, phone number, and starting balance
    	//---------------------------------------------------------------------------------------------
    	public Account (String owner, long account, String phone, double initial)
    	{
    		name = owner;
    		acctNumber = account;
    		phoneNumber = phone;
    		balance = initial;
    	}
     
    	//-----------------------------------------------------------------------
    	// Returns balance
    	//-----------------------------------------------------------------------
    	public double getBalance()
    	{
    		return balance;
    	}
     
    	//----------------------------------------------------------------------
    	// Returns account number
    	//----------------------------------------------------------------------
    	public long getAcctNumber()
    	{
    		return acctNumber;
    	}
     
    	//--------------------------------------------------
    	// deposits money into the account
    	//--------------------------------------------------
    	public double accDeposit (double amount)
    	{
    		balance = balance + amount;
    		return balance;
    	}
     
    	//-------------------------------------------------
    	//withdraws money from the account
    	//-------------------------------------------------
    	public double accWithdraw (double amount)
    	{
    		balance = balance - amount;
    		return balance;
    	}
     
    	//------------------------------------------------
    	//adds interest to the account
    	//------------------------------------------------
    	public double addInterest()
    	{
    		balance += (balance * RATE);
    		return balance;
    	}
     
    	//-------------------------------------------------
    	//Prints off the information for the account
    	//-------------------------------------------------
    	public String toString()
    	{
    		NumberFormat fmt = NumberFormat.getCurrencyInstance();
    		return name + "\t" + acctNumber + "\t" + phoneNumber + "\t" + fmt.format(balance);
    	}
    }

    The Bank class

    import java.util.Scanner;
    import java.util.ArrayList;
    import java.io.*;
     
    public class Bank
    {
    	private ArrayList<Account> bank;
    	private int count;
    	//---------------------------------------------------------
    	//Constructs the Bank
    	//---------------------------------------------------------
    	public Bank()
    	{
    		bank = new ArrayList<Account>();
    	}
     
    	//----------------------------------------------------------
    	//Creates a new Account
    	//----------------------------------------------------------
    	public void create (long accountNum,  String owner, String phNumber, double initial)
    	{
    			bank.add (new Account(owner, accountNum, phNumber, initial));
    			count++;
    	}
     
    	//-------------------------------------------------------------------------
    	//Desposits money into the account that matches the given account number
    	//-------------------------------------------------------------------------
    	public void deposit(long acctNum)
    	{
    		Scanner scan = new Scanner (System.in);
    		System.out.println("How much would you like to deposit into account " +acctNum+ "?");
    		double depositAmount = scan.nextDouble();
    		findAcct(acctNum).accDeposit(depositAmount);
     
    	}
     
    	//-----------------------------------------------------------------------
    	//withdraws money from the account that matches the given account number
    	//-----------------------------------------------------------------------
    	public void withdraw(long acctNum)
    	{
    		Scanner scan = new Scanner (System.in);
    		System.out.println("How much would you like to withdraw from account " +acctNum+ "?");
    		double withdrawAmount = scan.nextDouble();
    		findAcct(acctNum).accWithdraw(withdrawAmount);
    	}
     
    	//----------------------------------------------
    	//Adds interest to all accounts
    	//----------------------------------------------
    	public void Interest()
    	{
    		for (int i=0; i<count; i++)
    		 bank.get(i).addInterest();
    	}
     
    	public void load(String file) throws IOException
    	{
    		Scanner fileScan, accountScan;
    		String account, owner, phNumb;
    		long acctNumb;
    		double balance;
     
    		fileScan = new Scanner (new File(file));
     
    		while (fileScan.hasNext())
    		{
    			account = fileScan.nextLine();
     
    			accountScan = new Scanner(account);
    			do
    			{
    				owner = accountScan.next();
    				acctNumb = accountScan.nextLong();
    				phNumb = accountScan.next();
    				balance = accountScan.nextDouble();
     
    				create(acctNumb, owner, phNumb, balance);
    			}
    			while (accountScan.hasNext());
    		}
    	}
     
    	public void save(String file)throws IOException
    	{
    		PrintWriter out = new PrintWriter(new FileWriter(file));
    		out.println(toString());
    		out.close();
    	}
     
    	//---------------------------------------------------
    	//Prints out all the accounts and their information
    	//---------------------------------------------------
    	public String toString()
    	{
    		String report = "";
    		for (int i = 0; i<bank.size(); i++)
    			report += bank.get(i) + "\n";
     
    		return report;
    	}
     
    	//----------------------------------------------------
    	//Finds the specified account and returns the account
    	//----------------------------------------------------
    	private Account findAcct(long acctNum)
    	{
    		Account test = new Account();
    		for (int i=0; i<count; i++)
    		{
    			if (acctNum == bank.get(i).getAcctNumber())
    				test = bank.get(i);
    		}
     
    		return test;
     
    	}
    }

    and here is the bankdriver class

    import java.io.*;
     
    public class BankDriver
    {
    	public static void main (String[] args) throws IOException
    	{
    		Bank bank1 = new Bank();// creates bank
     
    		bank1.load("BankAccountsDocument.rtf");
    		bank1.deposit(1000);//deposits money to account 1000
    		bank1.withdraw(1001);//withdraws money from account 1001
    		bank1.Interest();// adds interest to all accounts
    		bank1.save("BankDocument.rtf");// prints all account information into the document
     
    		System.out.println(bank1);//prints out all accounts
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: InputMisMatchException

    That's way too much code to show for the problem you're having. The problem is presumably that you're trying to read in a long when the file contains something else. Try reading it in as a String and printing it out to see what it is.

    If you still can't figure it out, I suggest you provide an SSCCE (that's a link) that demonstrates the problem without all that extra code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: InputMisMatchException

    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: InputMisMatchException

    The java.util.InputMismatchException exception is pretty self explanatory.
    I would check to make sure your Scanner is reading in the correct variable types from the file.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    jmack (January 29th, 2011)

Similar Threads

  1. INPUTMISMATCHEXCEPTION
    By james in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 15th, 2010, 01:02 AM