blockasmash
blockasmash

Reputation: 3

Initialization and FileNotFoundException errors

I am a new Java student and am writing a program that consists of a main method, a class file, two input .txt files, and an output.txt file. The main method should ask the user what the account balance and annual interest is and import deposit and withdrawal information from their respective files, and then to display all calculations from the class file in an output file. I had originally written this file to ask users for all of this input using the scanner and now I'm trying to get it to work using files as input...which is not going so well.

Main method:

    import java.util.Scanner;
    import java.io.*;
    public class SavingsAccountDemo {

   public static void main(String[] args) {
   //declare variables
   double interest;
   double startAmount;
   double amountDeposit;
   double amountWithdraw;
   double d, w;
   String filenameInputW, filenameInputD, filenameOutput;
   PrintWriter oFile;
   File wFile, dFile;
   Scanner iFile;

   Scanner key = new Scanner(System.in);

    //get initial balance
   System.out.println("Enter initial account balance: ");
   startAmount = key.nextDouble();

   //create SavingsAccount class object sending starting amount to constructor
   SavingsAccount money = new SavingsAccount(startAmount);

   //get annual interest rate
   System.out.println("Enter annual interest rate: ");
   interest = key.nextDouble();

   //send interest rate to class
   money.setInterest(interest);

   //Retrieve withdrawals from withdrawal.txt
   filenameInputW="withdrawal.txt";
   wFile = new File (filenameInputW);
   iFile = new Scanner (wFile);
   while(iFile.hasNext())
   {
        double num;

        num=iFile.nextDouble();
        amountWithdraw += num;

        if(amountWithdraw >= 0.1)
        w++;
   }

   //send to SavingsAccount class
    money.withdraw(amountWithdraw);

   //Retrieve deposits from deposit.txt
   filenameInputD="deposit.txt";
   dFile = new File (filenameInputD);
   iFile = new Scanner (dFile);

   while(iFile.hasNext())
   {
        double num;

        num=iFile.nextDouble();
        amountDeposit += num;

        if (amountDeposit >= 0.1)
        d++;
   }

   //send to SavingsAccount class
    money.deposit(amountDeposit);

   //display retults
   filenameInputW="output.txt";
   oFile=new PrintWriter (filenameOutput);
   oFile.println("The ending balance is: " + money.getBalance());
   oFile.println("The total amount of withdrawls are: " + w);
   oFile.println("The total amount of deposists are: " + d);
   oFile.println("The annual interest rate is: " + money.getInterest());

}

}

My class file

/**
 * @(#)SavingsAccount.java
 *
 *
 * @author 
 * @version 1.00 2013/5/6
 */


public class SavingsAccount {

        //variables
        private double interest;
        private double balance;


        //Constructor
        public SavingsAccount(double b)
        {
            balance = b;
            interest = 0.0;
        }

        //Accessors
        public void setInterest(double i)
        {
            interest = i;
        }

        public void setBalance(double b)
        {
            balance = b;
        }

        //Mutators
        public double getInterest()
        {
            return interest;
        }

        public double getBalance()
        {
            return balance;
        }

        //Withdraw method
        public void withdraw(double withdraw)
        {
            balance = balance - withdraw;
        }

        //Deposit method
        public void deposit(double deposit)
        {
            balance = balance + deposit;
        }


        //Adding monthly interest to the balance
        public void addInterest()
        {
            double x = ((interest/12) * balance);
            balance = balance + x;
        }

}

I get these errors:

--------------------Configuration: --------------------
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:43: error: variable amountWithdraw might not have been initialized
            amountWithdraw += num;
            ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:46: error: variable w might not have been initialized
            w++;
            ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:50: error: variable amountWithdraw might not have been initialized
        money.withdraw(amountWithdraw);
                       ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:62: error: variable amountDeposit might not have been initialized
            amountDeposit += num;
            ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:65: error: variable d might not have been initialized
            d++;
            ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:69: error: variable amountDeposit might not have been initialized
        money.deposit(amountDeposit);
                      ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73: error: variable filenameOutput might not have been initialized
       oFile=new PrintWriter (filenameOutput);
                              ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:75: error: variable w might not have been initialized
       oFile.println("The total amount of withdrawls are: " + w);
                                                              ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:76: error: variable d might not have been initialized
       oFile.println("The total amount of deposists are: " + d);
                                                             ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:37: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
       iFile = new Scanner (wFile);
               ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:55: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
       iFile = new Scanner (dFile);
               ^
C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:73: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
       oFile=new PrintWriter (filenameOutput);
             ^
12 errors

Process completed.

I'm not looking for handouts, this is my first post for help here but I know you guys wont solve this for me...I just need to learn how to do this properly. I just have no idea why the initialization is having a problem (all mentioned variables are initialized) and the files are located in the same folder.

Upvotes: 0

Views: 450

Answers (4)

splrs
splrs

Reputation: 2432

Edit: For the wrongs...

You should be giving those variables values when you declare them. i.e. int w = 0 etc.

Dirty quick fix: As for the other exception-related errors, for simplicity you can just put public static void main(String[] args) throws FileNotFoundException to get things working in the interim.

Proper fix: Usually though, you should look to handle a FileNotFoundException in a catch block, as it's not good practice to rethrow exceptions willy-nilly.

Upvotes: 0

Rik Schaaf
Rik Schaaf

Reputation: 1183

I think you are confused about the difference between initialization and declaration. The variables that give errors are indeed declared, but they don't have an initial value.

Take a look at w++. This means: add 1 to w. But what is w? You didn't define that.

The same problem is true for d, amountWithdraw and amountDeposit

Also, new scanners with a file as input need a try-catch statement for a FileNotFoundException.

Finally, I think you meant filenameOutput="output.txt"; in stead of filenameInputW="output.txt";

Upvotes: 0

Birb
Birb

Reputation: 866

From your code you write the following at the very top: double amountWithdraw; and that's it. This is not an initialization, but a declaration.

The same applies if your girlfriend starts talking about kids, right? If she declares "I want kids" it's not the same as if she "initializes" (gives birth to) a baby. Both might be equally stressful, but in different ways.

You need to either type double amountWithdraw = 0.0; or add the following before modifying the value: amountWithdraw = 0.0;. That will fix the first of your handful of problems.

Upvotes: 0

user2167382
user2167382

Reputation: 346

--------------------Configuration: -------------------- C:\Users\Home\Documents\JCreator Pro\MyProjects\SavingsAccount\src\SavingsAccountDemo.java:43: error: variable amountWithdraw might not have been initialized amountWithdraw += num;

Well, you declared variable amountWithdraw as double, but compilator don't know what is value of this variable and he can't add num. So after declaration you need to initialize amountWithdraw. It can be like this:

amountWithdraw = 0;

It's so simple.

Upvotes: 0

Related Questions