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 22 of 22

Thread: How to import information from a file, and use it to calculate and display serval things?

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to import information from a file, and use it to calculate and display serval things?

    Hello everyone,

    I am trying to create a program that reads from a file, and outputs the data. The file contains a list of bank accounts with some standard bank account information, also we need to output what the monthly fee for each account is based on certain criteria. Then, after that we need to find the total of all deposits, the average of all accounts, and the average of the savings and the checking accounts located in the file, but this is to be done using polymorphism.

    I have gotten quite a lot of the program done, but I am having an issue in getting everything from this file to output correctly, and then to find the average of and total of everything that I need to.

    I have five classes all together, one I believe I have finished. I would like to start with analyzing this class, and then moving on.

    public class Bank
    {
    	protected int total;
     
    	public Bank()
    	{
    	BankAccount myBankAccount = new BankAccount();
     
    	System.out.println("There are: " + myBankAccount.getAccountCount() + myBankAccount.getAccountType() + " account in the Bank");
    	System.out.println("Account #: " + myBankAccount.getAccountNumber());
    	System.out.println("Account Type: " + myBankAccount.getAccountType());
    	System.out.println("CustomerName: " + myBankAccount.getCustomerName());
    	System.out.println("Customer Type: " + myBankAccount.getCustomerType());
    	System.out.println("Balance: " + myBankAccount.getBalance());
    	System.out.println("Monthly Fee: " + myBankAccount.getMonthlyFee());
    	}
    }

    The issue I am having with this class is that I am being told that I am not able to instantiate a BankAccount object. Have I done this right?

    Thank you,

    Techergy


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    being told that I am not able to instantiate a BankAccount object.
    Please copy the full text of the compiler's error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Yes, sorry about that. I just thought of that. Here is what I am seeing: Cannot instantiate the type
    BankAccount.

    Also here is the information that is located in the file that I am working with, if anyone would like to see:
    40239
    Checking
    Charles Xavier
    premier
    31000.35

    20925
    Savings
    Scott Summers
    Value
    345.89

    21354
    Savings
    Robert Drake
    advantage
    1235.23

    45687
    Checking
    Warren Worthington
    Advantage
    675.98

    43234
    Checking
    Henry McCoy
    Premier
    24576.76

    21356
    Savings
    Jean Grey
    Premier
    12678.45

    40123
    Checking
    Lorna Dane
    value
    1105.78

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Where is the definition for the BankAccount class? Did it compile without any errors?

    BTW What you posted is not the full text of the compiler's error message. The full message has more info.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Cannot instantiate the type BankAccount
     
    	at KManchette_Project3.Bank.<init>(Bank.java:9)
    	at KManchette_Project3.BankApp.main(BankApp.java:7)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Where is the definition for the BankAccount class? Did it compile without any errors?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    The BankAccount class did not compile with any errors. And to think, I was just thinking about add that.

    import java.util.Scanner;
     
    public abstract class BankAccount
    {
    	protected String accountType; // Saving, Checking, etc.
    	protected String customerName;
    	protected double balance;
    	protected int accountNumber;
    	protected String customerType; // Value, etc.
    	protected static int accountCount = 0;
    	protected Scanner readTheFile;
    	protected double monthlyFee;
     
    	public BankAccount() // Default constructor
    	{
    		accountType = "";
    		balance = 0.0;
    		accountNumber = 0;
    		customerName = "";
    		customerType = "";
    		accountCount++;
    	}
    	public BankAccount(String at, String cn, double b, int an, String ct) // 5-arg Constructor
    	{
    		accountType = at;
    		customerName = cn;
    		balance = b;
    		accountNumber = an;
    		customerType = ct;
    		accountCount++;
    	}
    	public void getFileInfo()
    	{
    		Scanner readTheFile = new Scanner("User/KaylaManchette/Documents/workspace/CSC 151/src/KManchette_Project3/AcctInfo.txt"); //creates a Scanner object to read the file
    		while (readTheFile.hasNextLine()){
    			accountNumber = readTheFile.nextInt(); 
    			customerType = readTheFile.next();
    			customerName = readTheFile.next();
    			balance = readTheFile.nextDouble();
    			customerType = readTheFile.next();
    		}
         }
    	public double getMonthlyFee()
    	{
     
            if(balance < 1000 && accountType.equals("Checking") && customerType.equals("Advantage"))
            {
                return 10.0;
            }
            else if(balance < 1500 && accountType.equals("Checking") && customerType.equals("Value"))
            {
                return 5.0;
            }
            else if(balance < 25000 && accountType.equals("Checking") && customerType.equals("Premier"))
            {
                return 30.0;
            }
            else if(balance >= 25000 && accountType.equals("Checking") && customerType.equals("Premier"))
            {
                return 0.0;
            }
            else
            {
                return 0.0;
            }
    	}
     
    	//Methods
    		// Getters
    		public String getAccountType() {return accountType;}
    		public String getCustomerName() {return customerName;}
    		public double getBalance() {return balance;}
    		public int getAccountNumber() {return accountNumber;}
    		public String getCustomerType() {return customerType;}
    		public static int getAccountCount()	{return accountCount;}
     
    		//Setters
    		public void setAccoutType(String at) {accountType = at;}
    		public void setCustomerName(String cn) {customerName = cn;}
    		public void setBalance(int b) {balance = b;}
    		public void setAccountNumber(int an) {accountNumber = an;}
    		public void setCustomerType(String ct) {customerType = ct;}
    		@Override
    		public String toString()
    		{
    			return "Account #: " + accountNumber
    			+ "\n" + "Account Type: " + accountType + "\n" + "Customer Type: " + customerType + "\n"
    			+ "Customer Name: " + customerName + "\n" + "Balance: " + balance + "\n" + "Monthly Fee: " + getMonthlyFee();
    		}
     
    }// End BankAccount
    I do have one question about this class though, when I create the constructors, and then read from the file; have I called it right so that I will display the information that I want to?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    will display the information that I want to?
    Compile it, execute it and see what it outputs.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    I have, it only outputs the empty strings located in the constructor

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Where is the code that is supposed to assign values to those variables? Is it being executed? Do some desk checking of the code to see where the assignments are made and then back track to see where those methods are being called from.
    Also try debugging the code by adding lots of println statements that print messages as methods are executed and that print the values of the variables that control the execution flow. The print out will help you understand how the code is executing and why it is executing the way it does.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    This is where I should have stored the variables from the file.
    public void getFileInfo()
    	{
    		Scanner readTheFile = new Scanner("User/KaylaManchette/Documents/workspace/CSC 151/src/KManchette_Project3/AcctInfo.txt"); //creates a Scanner object to read the file
    		while (readTheFile.hasNextLine()){
    			accountNumber = readTheFile.nextInt(); 
    			customerType = readTheFile.next();
    			customerName = readTheFile.next();
    			balance = readTheFile.nextDouble();
    			customerType = readTheFile.next();
    		}

    I have been told that even though I instantiated the default constructor the way I did, the constructor still should have been overridden when I wrote the while loop. Then, it should have outputted correctly when I wrote the println statements in the Bank class from the getters I wrote at the end of the BankAccount class. Would I need to, in some way, write the constructor so that it looks something like readTheFile.getAccountName?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Are the assignment statements that assign values to the class variables being executed? If they are not executed then the variables values won't be changed. Add some println statements to print a message when the assignment statements are executed. If there is no print out then the assignment statements are not being executed.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    It looks like I cannot figure out why information is not displaying correctly until I figure out why I cannot instantiate the BankAccount object. I am writing in correctly, right? BankAccount myBankAccount = new BankAccount();

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    public abstract class BankAccount
    You cannot instantiate an abstract class.
    Improving the world one idiot at a time!

  15. The Following User Says Thank You to Junky For This Useful Post:

    Techergy (May 4th, 2013)

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    why I cannot instantiate the BankAccount object.
    Are you getting any errors? If there are no errors then code must be executing.
    Did you add any calls to the println method to the constructor and the methods that you want called?
    Did any of them print out any messages? If they didn't print any messages, then the constructors and methods are not being called.

    Post the code with the println statements and post the contents of the console from when you execute the program that shows what the program prints out when it executes.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Oh! urrh right, I forgot I put that there and that I could not instantiate from an abstract class.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    G** I hate the IDEs these OPs use. The error messages don't give out information about what the error is.
    When I use javac with the code I get this error message:
     
    Bank.java:13: error: BankAccount is abstract; cannot be instantiated
    	BankAccount myBankAccount = new BankAccount();
                                            ^
    This error message clearly points out the error that I missed because I expected the posted error message would show the error. Stupid me. And a glass of wine with dinner.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    Yeah sorry about that, now that that is fixed. I can see now that I am not proper calling the variables I have used in the while loop to store variables from the file like I want to.

    I am going to keep working on this and see if I can see what I need to do. I will update if I find out how I should do this or if I run into any more issues.

    Thank you for your help so far.

  20. #19
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Override default constructor with information stored from a file

    Hello everyone,

    I am trying to read information from a file, and then have the information output. I have already posted this another section of this forum, and I apologize for that but I am hoping I can find someone a different take on Object Oriented Programming.

    So, here is the code I am having problems with
    public BankAccount() // Default constructor
    	{
    		accountType = "";
    		balance = 0.0;
    		accountNumber = 0;
    		customerName = "";
    		customerType = "";
    		accountCount++;
    	}
    	public BankAccount(String at, String cn, double b, int an, String ct) // 5-arg Constructor
    	{
    		accountType = at;
    		customerName = cn;
    		balance = b;
    		accountNumber = an;
    		customerType = ct;
    		accountCount++;
    	}
    	public void getFileInfo()
    	{
    		Scanner readTheFile = new Scanner("F:\\KManchette_Project3\\AcctInfo.txt"); //creates a Scanner object to read the file
    		while (readTheFile.hasNextLine())
    		{
    			accountNumber = readTheFile.nextInt(); 
    			customerType = readTheFile.next();
    			customerName = readTheFile.next();
    			balance = readTheFile.nextDouble();
    			customerType = readTheFile.next();
    		}
    I have heard that you can override a default constructor by storing information from a file in the same variables that the default constructor uses. Though, for some reason when I try to run my code, the program will only output the empty information that is located inside of the default constructor. No errors have occurred, the program is just not displaying what I want it to. Have I written the default constructor and the file reader correctly so that they can work with each other? Do I specifically need to call the file reader in the default constructor with something like this?
     public Bank()
    {
    [INDENT]accountNumber = readTheFile.accountNumber[/INDENT]
    }

    Techergy

  21. #20
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Override default constructor with information stored from a file

    Quote Originally Posted by Techergy View Post
    I have already posted this another section of this forum, and I apologize for that but I am hoping I can find someone a different take
    There is no need to double post on a forum. Ever.
    The same people who read posts in one thread, read the posts in other threads.
    Threads merged

  22. #21
    Junior Member
    Join Date
    May 2013
    Posts
    11
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    I have gotten this program done. But I still have one issue, when I try to call the file in my main method that looks like this:

    public class BankApp
    {
    	public static void main(String[] args)
    	{
    		Bank fileReader = new Bank(new file("AcctInfo.txt"));  
     
    		fileReader.readFromFile();
    		fileReader.showInfo();
     
    	}//End main
    }//end class

    I receive my created exception stating that the file could not be found. All information is basically coming from this class:
    import java.io.*;
    import java.text.DecimalFormat;
    import java.util.*;
     
    public class Bank
    {
    	//An array of BankAccount objects
    	public ArrayList<BankAccount> bankAccountArray = null;
     
    	private File fileObject = null; 
    	private Scanner reader = null;  //Declare scanner object 
    	private DecimalFormat df = null;
     
    	//Construtors
    	public Bank(String fileName) //No default constructor 
    	{
     
    		bankAccountArray = new ArrayList<BankAccount>();
    		fileObject = new File(fileName);
     
    		//Check for file
    		try
    		{
    			reader = new Scanner(fileObject);
    		}
    		catch(FileNotFoundException e) //If cannot create file. display cannot and close program
    		{
    			System.out.println("This file: " + fileName + " does not exist. Program will close.");
    			System.exit(1);
    		}
    	}//End Construtors
     
    	//Methods
    	public void fileInfo()
    	{
    		while(reader.hasNext())
    		{
    			bankAccountArray.add(readFromFile());
    		}
    	}//End readFile
     
    	public BankAccount readFromFile() 
    	{
    		//Get info from file
    		String accountNumber = reader.nextLine();
    		String accountType = reader.nextLine();
    		String customerName = reader.nextLine();
    		String customerType = reader.nextLine();
    		double balance = reader.nextDouble();
    		reader.nextLine(); //To clear buffer
     
    	 //Create subclass objects with 4-arg constructor
    		BankAccount AccountObject = callSub(accountNumber,accountType,customerName,customerType,balance);
    		{
    			return AccountObject;
    		}
    	}//End readFromFile
     
    		//overloaded 5-arg constructor	
    	private BankAccount callSub(String accountNumber,String accountType,String customerName,String customerType,double balance)
    	{
    		if(accountType.equalsIgnoreCase("savings"))
    		return  new SavingsAccount(accountNumber, customerName, customerType, balance);
    			else return	 new CheckingAccount(accountNumber, customerName, customerType, balance);
    	}
     
    	public void showInfo()
    	{
    		double tot = SavingsAccount.getTotal();
    		int ccc = SavingsAccount.getAccountCounter3();
    		double tot2 = CheckingAccount.getTotal();
    		int cc = CheckingAccount.getAccountCounter2();
    		double total = 0.0;
    		df = new DecimalFormat("#,##0.00");
     
    		//Display each object in arrayList and add total of balance
    		for(int k = 0; k < bankAccountArray.size(); k++)
    		{
    			System.out.println(bankAccountArray.get(k));
    			total += bankAccountArray.get(k).getBalance();
    		}
     
     
    		System.out.println("The total amount of deposits is: $" + df.format(total));
    		System.out.println("The average balance is: $" + df.format(total / bankAccountArray.size()));
    		System.out.println("There were "+ ccc +" savings accounts created."+
    				"\nThe total for savings accounts is: $" + df.format(tot)+
    				"\nThe average savings account  balance is: $" + df.format(tot/ccc));
    		System.out.print("There were "+ cc +" checking accounts created."+
    				"\nThe total for checking accounts is: $" + df.format(tot2)+
    				"\nThe average checking account  balance is: $" + df.format(tot2/cc));
    	}//End showInfo
     
     
    }//End class
    Now, as I think I have stated before; there are 3 other classes but they all dumped into this one class. If you want to see them I would be more then happy to post them.

    So, why am I not able to see the file? The file is located in the same folder as the rest of the program. I have also tried to point the program to the absolute path to the file. Which, since I am on a Mac should be /Users/MyUserName/Documents/workspace/CSC_151/MyNameHere_Project3/AcctInfo.txt. Does anyone know if that is correct? It's the only thing I'm seeing that could be correct.

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to import information from a file, and use it to calculate and display serval things?

    To see where the program is looking for the file, print out the absolute path (see File class API doc for the method name) of the fileObject variable.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Import text file to mysql
    By jayraj in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 6th, 2013, 06:45 PM
  2. Import text file to mysql
    By jayraj in forum JDBC & Databases
    Replies: 6
    Last Post: April 6th, 2013, 06:41 PM
  3. loading things from a text file into an array list
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2011, 03:32 PM
  4. how to import my own class in my .java file
    By amr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 09:31 PM
  5. Something wrong with the import things
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 25th, 2010, 07:30 AM