Importing data into an array and save textarea to file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Importing data into an array and save textarea to file

  1. #1
    Join Date
    Mar 2006
    Posts
    1

    Importing data into an array and save textarea to file

    Here is code for a GUI Mortgage program I am working on for a class assignment. There are two things I need help with.

    (1) I need to populate the array with data from a sequential file. This means I would place the first three elements of my current array into a text file, but I need to keep my fourth element intact because that is driven by user input via the interface.

    (2) I need to be able to save the amortization schedule, that gets displayed in the TexArea, to a text file. I have added a save button to the GUI interface that I would like to use for saving this information.

    Any assistance is greatly appreciated.

    //Import java packages
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.lang.*;
    import java.text.NumberFormat;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;


    public class GUIMortgageCalculatorWk5 extends JFrame implements ActionListener
    {
    // Labels
    JLabel AmountLabel = new JLabel( "Mortgage Amount:$ " ); // Declares Mortgage Amount Label
    JLabel PaymentLabel = new JLabel( "Monthly Payment: " ); // Declares Monthly Payment Label
    JLabel InterestLabel = new JLabel( "Interest Rate %: " ); // Declares Interest Rate Label
    JLabel TermofLoanLabel = new JLabel( "TermofLoan of Loan: " ); // Declares TermofLoan of Loan Label

    // Text Fields
    JTextField mortgageAmount = new JTextField(7); // Declares Mortgage Amount Text Field
    JTextField MonthlyPayment = new JTextField(7); // Declares Monthly Payment Text Field
    JTextField InterestRate = new JTextField(7); // Declares Interest Rate Text Field
    JTextField TermofLoan = new JTextField(7); // Declares TermofLoan of Loan Text Field


    // Buttons
    JButton Loan1 = new JButton( "7 years at 5.35%" ); // Declares 1st Mortgage TermofLoan and Interest Rate
    JButton Loan2 = new JButton( "15 years at 5.50%" ); // Declares 2nd Mortgage TermofLoan and Interest Rate
    JButton Loan3 = new JButton( "30 years at 5.75%" ); // Declares 3rd Mortgage TermofLoan and Interest Rate
    JButton ExitButton = new JButton( "Exit" ); // Declares Exit Button
    JButton ClearButton = new JButton( "Clear" ); // Declares New Calculation Button
    JButton CalculateButton = new JButton( "Calculate" ); // Declares New Calculation Button
    JButton SaveButton = new JButton( "Save" ); // Declares New Save Button
    JButton ChartButton = new JButton( "Show Chart" ); // Declares New Chart Button

    // Text Area and Scroll
    JTextArea MortgageTable = new JTextArea(25,40); // Declares Mortgage Table Area
    JScrollPane scroll = new JScrollPane(MortgageTable); // Declares ScrollPane and puts the Mortgage Table inside


    GUIMortgageCalculatorWk5()//Method
    {
    //Frame, Panel, and Layout set up
    super("GUI Mortgage Calculator");
    setSize(600, 400);
    setLocation(0, 0);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pane = new JPanel();
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

    //Setup container and contents
    Container grid = getContentPane();
    grid.setLayout(new GridLayout(4,0,5,5));
    pane.add(grid);
    pane.add(scroll);
    grid.add(AmountLabel);
    grid.add(mortgageAmount);
    grid.add(InterestLabel);
    grid.add(InterestRate);
    grid.add(TermofLoanLabel);
    grid.add(TermofLoan);
    grid.add(PaymentLabel);
    grid.add(MonthlyPayment);
    grid.add(Loan1);
    grid.add(Loan2);
    grid.add(Loan3);
    grid.add(SaveButton);
    grid.add(CalculateButton);
    grid.add(ClearButton);
    grid.add(ExitButton);
    grid.add(ChartButton);
    MonthlyPayment.setEditable(false);
    setContentPane(pane);
    setVisible(true);

    //Adds Action Listeners
    ExitButton.addActionListener(this);
    ClearButton.addActionListener(this);
    Loan1.addActionListener(this);
    Loan2.addActionListener(this);
    Loan3.addActionListener(this);
    mortgageAmount.addActionListener(this);
    InterestRate.addActionListener(this);
    TermofLoan.addActionListener(this);
    MonthlyPayment.addActionListener(this);
    CalculateButton.addActionListener(this);


    }//End GUIMortgageCalculatorWk5 method


    public void actionPerformed(ActionEvent e)
    {
    Object command = e.getSource();
    if
    (
    command == ExitButton) //sets ExitButton
    System.exit(0); // Exits from exit button

    int loanTermofLoan = 0; // Declares loanTermofLoan

    if (command == Loan1) //Activates the 1st Loan Button
    {
    loanTermofLoan = 0; //Sets 1st value of Array
    TermofLoan.setText("0");
    InterestRate.setText("0");
    }

    if (command == Loan2) //Activates the 2nd Loan Button
    {
    loanTermofLoan = 1; //Sets 2nd value of Array
    TermofLoan.setText("0");
    InterestRate.setText("0");
    }

    if (command == Loan3) //Activates the 3rd Loan Button
    {
    loanTermofLoan = 2; // Sets 3rd value of Array
    TermofLoan.setText("0");
    InterestRate.setText("0");
    }

    if (command == CalculateButton ) //Activates the Calculate Button for manual entries
    {
    loanTermofLoan = 3; // Sets 4rd value of Array
    }

    double t1 = Double.parseDouble(TermofLoan.getText());
    double r1 = Double.parseDouble(InterestRate.getText());
    double [][] loans = { {7, 5.35}, {15, 5.50}, {30, 5.75}, {t1, r1} }; // Array Data for Calculation

    double mortgage = 0; // Declares and Initializes mortgage
    mortgage = Double.parseDouble(mortgageAmount.getText()); //Parse text to double data type for calculation
    double interestRate = loans [loanTermofLoan][1]; // Sets interestRate amount
    double intRate = (interestRate / 100) / 12; // Calculates Interst Rate
    double loanTermofLoanMonths = loans [loanTermofLoan] [0]; // Calculates Loan TermofLoan in Months
    int months = (int)loanTermofLoanMonths * 12; // Converts Loan TermofLoan to Months
    double interestRateMonthly = (intRate / 12); // Converts Annual interest rate to monthly interst rate
    double payment = mortgage * intRate / (1 - (Math.pow(1/(1 + intRate), months))); // Calculation for Monthly payment
    double remainingLoanBalance = mortgage; // Sets Reamaining Balance
    double MonthlyPaymentInterest = 0; // holds current interest payment
    double MonthlyPaymentPrincipal = 0; // holds current principal payment

    // Number formatter to format output in table
    NumberFormat CurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US); //Display amounts in US Dollars
    MonthlyPayment.setText(CurrencyFormatter.format(payment));
    MortgageTable.setText("Month\tPrincipal\tInterest\tEnding Balance\n" + // Formats TextArea Header
    "----------\t------------\t-------------\t----------------------\n"); // Formats TextArea Header cont.

    for (;months > 0 ; months -- )
    {
    //Append loop for mortgage detail in the text area
    MonthlyPaymentInterest = (remainingLoanBalance * intRate);//Monthly Payment Toward Interest
    MonthlyPaymentPrincipal = (payment - MonthlyPaymentInterest);//Monthly Payment Toward Principal
    remainingLoanBalance = (remainingLoanBalance - MonthlyPaymentPrincipal);//Remaining loan Balance

    MortgageTable.setCaret (new DefaultCaret()); // Sets Scroll position to the top left corner
    MortgageTable.append(String.valueOf(months) + "\t" +
    CurrencyFormatter.format(MonthlyPaymentPrincipal) + "\t" +
    CurrencyFormatter.format(MonthlyPaymentInterest) + "\t" +
    CurrencyFormatter.format(remainingLoanBalance) + "\n");//
    }

    //New calculation button
    if(command == ClearButton)
    {
    mortgageAmount.setText(null); //clears mortgage amount fields
    MonthlyPayment.setText(null); //clears monthly payment fields
    InterestRate.setText(null); //clears interest field
    TermofLoan.setText(null); //clears rate field
    MortgageTable.setText(null); //clears mortgage table
    }

    } // End actionPerformed method


    public static void main(String[] args)
    {
    new GUIMortgageCalculatorWk5();
    }

    }//End Class

  2. #2
    Join Date
    Mar 2006
    Posts
    35
    Here's one problem with your code MonthlyPayment.setText(CurrencyFormatter.format(pa yment)); you need to make it payment with no space. Another thing is that you have done no error checking for instance did you know on your application if you press one of the years at buttons then backspace the loan ammount the clear button will not work you need to fix that. The're various other places you need error checking. You need to fix those before you can worry about your two questions. How long do you have till you have to turn in the assignment I might be able to help you some.

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What methods can you use to read content from a file? How do you process that content so that it can be used to provide data values?

    How do you write content to a file from a TextArea?

  4. #4
    Join Date
    Mar 2006
    Posts
    35
    you'll have to use serializable objects to read data and maintain it's value like int string etc. I'm a novice programmer myself so I'm probably not going to be as much help as others at explaining it. Here's one link that might get you started http://www.cs.wustl.edu/~kjg/cs102/Notes/StreamsNFiles/ I would just search for information on serializable sequential files in java something like that. I've done it before many times but it's kinda complicated to explian in a forum. As for writing to a file from your textarea you could once again use serilized object or just recalculate your numbers and write them to a file without serializing them if you don't have to open the file again where the data has to have meaning once agian int string etc. I don't know if that's any help if not sorry like I said I'm a semi newbie myself.

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I don't think you need to serialize the text in the text area. Just think about a "toString" method and insert it into a process which will allow you to write the string to a file.

  6. #6
    Join Date
    Mar 2006
    Posts
    35
    Yea that's what i said you don't have to if you just want text that doesn't have to be used again like opened up and have calculations preformed on it.

  7. #7
    Join Date
    Mar 2006
    Posts
    35
    I also noticed this morning when looking at your application that your months are backwards. Just letting you know cuz if i noticed your teacher probably will too.

  8. #8
    Join Date
    Mar 2006
    Posts
    35
    here you go the original person that posted for help has not said anything sense so oh well. However I did make a mock up loanamortization program and posted it to a website i was making for someone else I'm not very good with html so forgive the cruedness of it here it is www.freewebs.com/verbalassasiin/index.htm enjoy.
    Last edited by blastmaster; 03-07-2006 at 04:43 PM.

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2008, 11:17 AM
  2. Saving data in a binary file
    By teja8100 in forum VB Classic
    Replies: 6
    Last Post: 07-08-2005, 06:03 PM
  3. Registry question: Save and read binary data
    By Paulo Costa in forum VB Classic
    Replies: 4
    Last Post: 07-31-2001, 01:15 PM
  4. Registry question: Save and read binary data
    By Paulo Costa in forum VB Classic
    Replies: 0
    Last Post: 07-30-2001, 05:15 PM
  5. Save EventMsg to a file
    By Ranger in forum VB Classic
    Replies: 0
    Last Post: 12-09-2000, 01:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center