Results 1 to 8 of 8
  1. #1
    Techergy is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Problem with reading from file

    Hello,

    I am trying to create a program that will read the bank account information of a group of people from a file, and then print that information as well as tell the person what they owe in monthly fees.

    Here is the code I have so far:
    Java Code:
    // This program reads ouput from a file while calculating the monthly fee and amount deposited in a persons bank account.
    
    import java.io.*;
    import java.util.Scanner;
    
    public class bank
    {
    	public static void main(String[] args) throws FileNotFoundException
    	{
    		Double amountInAccount;
    		String line;
    		
    		File file = new File("AcctInfo.txt");
    		Scanner input = new Scanner(file);
    		
    		while (input.hasNext()) 
    		{
    			line = input.nextLine();
    			
    			amountInAccount = input.nextDouble();
    			System.out.println("You have" + " " + amountInAccount);
    			System.out.print("So," + " ");
    		
    			if (amountInAccount < 1500) 
    			{
    				System.out.print("You owe $5 in monthly fees");
    			}
    			else if (amountInAccount >= 1500) 
    			{
    				System.out.print("You owe $0 in monthly fees");
    			}
    			else if (amountInAccount < 1000) 
    			{
    				System.out.print("You owe $10 in monthly fees");
    			}
    			else if (amountInAccount >= 1000) 
    			{
    				System.out.print("You owe $0 in monthly fees");
    			}
    			else if (amountInAccount < 25000) 
    			{
    				System.out.print("You owe $30 in monthly fees");
    			}
    			else if (amountInAccount >= 25000) 
    			{
    				System.out.print("You owe $0 in monthly fees");
    			}
    			else 
    			{
    				System.out.print("You owe $0 in monthly fees");
    			}
    		} // End While
    			
    		input.close();
    		
    	} // End main
    	
    } // End class
    Any help would be greatly appreciated, the program is a little disjointed and doesn't work, I get this exception when I run the program.
    Problem with reading from file-screen-shot-2013-02-23-2.08.33-pm.png

    Oh, and the amount of monthly fee the person owes is based on the type of account the person (savings or checking), the level of account the person has (i.e. value, advantage, or premiere) and the amount of money in the account. I only knew how to call the amount by how much money the person had in the account.

    Thank you.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Problem with reading from file

    You should post the text file as well. The problem is at line 20, "amountInAccount = input.nextDouble();", it's trying to read a double but cannot. The format of the input is probably incorrect.

  3. #3
    Techergy is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem with reading from file

    Yes I'm sorry

    Here is the text file https://www.dropbox.com/s/b1qr9m5br9vvy4l/AcctInfo.txt

    So, if the input is incorrect; should I just remove that line and keep lines 21 and 22, and should that print what I want?

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Problem with reading from file

    Not exactly, that would make amountInAccount null and you'd probably get a NullPointerException.

    First, notice how you read the line first: "input.nextLine()"

    I would use this instead of input.nextDouble()

    Java Code:
    amountInAccount = Double.parseDouble(line);
    But even then you will get some problems when the while loop goes to the next line. You need to deal with all of the lines in the file until the next number actually occurs.

  5. #5
    Techergy is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem with reading from file

    I have changed the code that you have suggested. If I want the program to read every line, should I just put an "input.nextLine()" until the number comes up; or is there a more efficient way to do this?

  6. #6
    Techergy is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem with reading from file

    So, I have been working on this a little and remembered that to have the information stored in the file print; I had to "System.out.println(line)" which just by itself works and will print out everything stored in the file, but when I add back in the if-else statements to let the user know how much there monthly fee is, all I get is this:
    Problem with reading from file-screen-shot-2013-02-24-9.58.39-am.png

    What else am I missing? I can feel that I'm close to having this code done, but just cannot see what still is causing this program to crash.
    Last edited by Techergy; 02-25-2013 at 12:40 AM.

  7. #7
    Techergy is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Problem with reading from file

    Bump

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem with reading from file

    You are reading in the word "Checking" and trying to parse it to a double.
    Not sure what else can be said.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Problem in reading from file. Please help.
    By Iman in forum Advanced Java
    Replies: 4
    Last Post: 05-21-2012, 02:02 PM
  2. reading a file problem
    By aianta in forum New To Java
    Replies: 3
    Last Post: 08-03-2011, 07:22 PM
  3. problem reading file
    By jmoutia in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 03:19 AM
  4. Problem reading from a file
    By sarapeace in forum New To Java
    Replies: 13
    Last Post: 10-03-2010, 02:08 PM
  5. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •