File Input Stream

Getting Data from a file

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2826 Views - Last Post: 18 September 2010 - 10:00 PM Rate Topic: -----

#1 admum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 10

File Input Stream

Posted 16 September 2010 - 08:33 PM

Hello
I am trying to retrieve data from a text file and place it in a text field.

I am not sure were I am going.

Problem is around 522 to 531?

/**
 *Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from 
 * user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans:
- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%
Use an array for the mortgage data for the different loans. Read the interest rates to fill the array from a sequential file.
Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. 
Add graphics in the form of a chart. Allow the user to loop back and enter a new amount and make a new selection or quit. 
Please insert comments in the program to document the program.
 * 
 */
import java.io.*;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.text.*;
import javax.swing.JOptionPane;

public class Main {

    
 
    public static void main(String[] args) {
        Calculater form = new Calculater();
        form.setVisible(true);
    }

}



public class Calculater extends javax.swing.JFrame {

    
// Variables
    String  termYearString="0", interestString="0", newLoan1 = "0",newLoan2="0",
            newLoan3="0", newSelection1 = "0";
     double principal = 0.0;
     int  termsYear = 0;
     double term = 0;
     double  interest = 0.0;
     double monthlyPayment = 0.0;
     double rate =0;
     double temp = 0;
     double InterestPaid = 0;
     double termMonths = 0;
     double payment = 0;
     double loanBalance = 0;
     double monthlyInterest =0;
     double payPrincipal =0;
     double newPrincipal =0;
     int month = 0;
     int totalMonth =0;

     


    /** Creates new form Calculater */
    public Calculater() {
        initComponents();
    }


   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        principalText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        calButton = new javax.swing.JButton();
        resetButon = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        termText = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        interestRateText = new javax.swing.JTextField();
        exitButton = new javax.swing.JButton();
        preMortComboBox = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        monthlyPaymentArea = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        amSchedArea = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        seveYearMort = new javax.swing.JMenuItem();
        fifYearMort = new javax.swing.JMenuItem();
        thirYearMort = new javax.swing.JMenuItem();
        rateFromFile = new javax.swing.JMenuItem();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mortgage Calculator");
        setBackground(new java.awt.Color(204, 204, 255));

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Principal of Mortgage");

        principalText.setText("0.00");
        principalText.setToolTipText("Enter Principal Here");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setText("Pick a Pre-defined Mortgage");

        calButton.setText("Calculate");
        calButton.setToolTipText("Click To Find Your Answer");
        calButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calButtonActionPerformed(evt);
            }
        });

        resetButon.setText("Reset");
        resetButon.setToolTipText("Click To Reset The Mortgage Calculator");
        resetButon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButonActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setText("Or Enter Your Own Information");
        jLabel5.setToolTipText("You can also edit the information after picking a pre-defined mortgage.");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Term of Mortgage (years)");

        termText.setText("0");
        termText.setToolTipText("Enter Number of Years");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Intrest Rate of Mortgage");

        interestRateText.setText("0.00");
        interestRateText.setToolTipText("Enter Interest Rate.");

        exitButton.setText("Exit");
        exitButton.setToolTipText("Click to cloase window");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        preMortComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select", "7 Years at 5.35%", "15 Years at 5.5%", "30 Years at 5.75%" }));
        preMortComboBox.setToolTipText("If You Choose, You Can Pick A Pre-defined Mortgage From This List.");
        preMortComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                preMortComboBoxActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel8.setText("Edit the boxes below to make the pre-defined mortgage information suite your needs.");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(205, 205, 205)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(principalText, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(preMortComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jLabel1))
                        .addGap(92, 92, 92))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addContainerGap(301, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(termText, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(22, 22, 22)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(interestRateText, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(46, 46, 46))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(calButton)
                                .addGap(109, 109, 109)
                                .addComponent(resetButon)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 145, Short.MAX_VALUE)
                                .addComponent(exitButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(29, 29, 29)))
                        .addGap(32, 32, 32))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(principalText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(preMortComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addComponent(jLabel5)
                .addGap(11, 11, 11)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(termText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(interestRateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(calButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(resetButon)
                    .addComponent(exitButton))
                .addContainerGap())
        );

        jPanel2.setBackground(new java.awt.Color(0, 204, 204));
        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel6.setText("Results");

        monthlyPaymentArea.setColumns(20);
        monthlyPaymentArea.setRows(5);
        jScrollPane1.setViewportView(monthlyPaymentArea);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Amortization Schedule");

        amSchedArea.setColumns(20);
        amSchedArea.setRows(5);
        jScrollPane2.setViewportView(amSchedArea);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
                    .addComponent(jLabel7)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(304, Short.MAX_VALUE))
        );

        jMenu1.setBackground(new java.awt.Color(255, 255, 255));
        jMenu1.setText("Pick A Pre-defined Mortgage");
        jMenu1.setToolTipText("Pick A Pre-defined Mortgage");

        seveYearMort.setText("7 Years at 5.35%");
        seveYearMort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                seveYearMortActionPerformed(evt);
            }
        });
        jMenu1.add(seveYearMort);

        fifYearMort.setText("15 Years at 5.5%");
        fifYearMort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fifYearMortActionPerformed(evt);
            }
        });
        jMenu1.add(fifYearMort);

        thirYearMort.setText("30 Years at 5.75%");
        thirYearMort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                thirYearMortActionPerformed(evt);
            }
        });
        jMenu1.add(thirYearMort);

        rateFromFile.setText("Rate From File");
        rateFromFile.setToolTipText("This is a special rate that may change at any time.");
        rateFromFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rateFromFileActionPerformed(evt);
            }
        });
        jMenu1.add(rateFromFile);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents





    private void calButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calButtonActionPerformed
       
        DecimalFormat formatter = new DecimalFormat("#0.00"); // To display both cents columns
        try {
           String ir = principalText.getText();
             principal = Double.valueOf(ir.trim()).doubleValue();


            }
            catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Bad Number Entered into principal Field",
                    "Error", JOptionPane.ERROR_MESSAGE);
        return;
        }
       try {
            termsYear = Integer.parseInt(
                    this.termText.getText());
        }
            catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Bad Number Entered into Term Field",
                    "Error", JOptionPane.ERROR_MESSAGE);
        return;
        }
     try {
            String ir = interestRateText.getText();
             interest = Double.valueOf(ir.trim()).doubleValue();


     
        }
            catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Bad Number Entered into interest Rate Field",
                    "Error", JOptionPane.ERROR_MESSAGE);
        return;
        }
      // This formula will calculate the payment information.
        term = termsYear * 12;
       rate = interest / 1200;

            monthlyPayment = (rate + rate/(Math.pow(1+rate, term) -1)) * principal; // Formula provided by YI MI

    monthlyPaymentArea.setText(
               "\t The loan amount is   $" +formatter.format(principal)+""
              + "\n\t The term of the loan is    "+ termsYear +"  years"
              + "\n\t The interest rate applied to the loan is   " +formatter.format(interest)+" %"
              + "\n\t The monthly payment is   $" +formatter.format(monthlyPayment)  );
       

    amSchedArea.append("  Month      Principal Payment     Interest Paid         New Principal\n");

       while (principal > 1) {
     temp=(1-1/Math.pow((1+rate), month));
     monthlyInterest = principal * rate;
     payPrincipal = monthlyPayment - monthlyInterest;
     newPrincipal = principal - payPrincipal;
     principal = newPrincipal;
     totalMonth = month + 1;
     month = totalMonth;

 
    amSchedArea.append("     "+(month ) + "\t"+ (formatter.format(payPrincipal)+ "\t  " + (formatter.format(monthlyInterest)+ "\t    " + (formatter.format(principal) + "\n"))));
 

    }


    }
// Reset Button
    private void resetButonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButonActionPerformed
    

        principalText.setText("0.00");
       termText.setText("0");
        interestRateText.setText("0.0");
        monthlyPaymentArea.setText ("");
        amSchedArea.setText("");
        preMortComboBox.setSelectedIndex(0);

    }
// Exit Button
    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        System.exit(0);
    }

    private void preMortComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preMortComboBoxActionPerformed
       String value = preMortComboBox.getSelectedItem().toString();
        if (value.contentEquals("7 Years at 5.35%")) {
           termYearString="7";
           interestString="5.35";
           termText.setText(termYearString);
           interestRateText.setText(interestString);
        } else {
           if (value.contentEquals("15 Years at 5.5%")) {
           termYearString="15";
           interestString="5.5";
           termText.setText(termYearString);
           interestRateText.setText(interestString);
        } else {
          if (value.contentEquals("30 Years at 5.75%")) {
           termYearString="30";
           interestString="5.75";
           termText.setText(termYearString);
           interestRateText.setText(interestString);

        }
         }
        }
      



    }

    private void seveYearMortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_seveYearMortActionPerformed
       termYearString="7";
       interestString="5.35";
       termText.setText(termYearString);
       interestRateText.setText(interestString);
    }

    private void fifYearMortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fifYearMortActionPerformed
       termYearString="15";
       interestString="5.5";
       termText.setText(termYearString);
       interestRateText.setText(interestString);
    }

    private void thirYearMortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_thirYearMortActionPerformed
       termYearString="30";
      interestString="5.75";
      termText.setText(termYearString);
      interestRateText.setText(interestString);
    }

    private void rateFromFileActionPerformed(java.awt.event.ActionEvent evt) {
   

       //create file object

      File file = new File("rate.txt");
      int ch;
      StringBuffer strContent = new StringBuffer("");
      FileInputStream fin = null;
      try
      {
      fin = new FileInputStream(file);
       while( (ch = fin.read()) != -1)
       strContent.append((char)ch);
       fin.close();
       }
      catch(FileNotFoundException e)
      {
     JOptionPane.showMessageDialog(this, "File " + file.getAbsolutePath() + " could not be found on filesystem",
                    "Error", JOptionPane.ERROR_MESSAGE);
    return;
      }
      catch(IOException ioe)
      {
     JOptionPane.showMessageDialog(this, "Exception while reading the file" + ioe,
                    "Error", JOptionPane.ERROR_MESSAGE);
        return;

      }
        /* ++++++++++++++++++++++++++++++++++++++++++++
         * So How do I get teh data from the file into the interestRateText area? 
         */
             interestString=(strContent;
 
       termYearString="30";
       termText.setText(termYearString);
       interestRateText.setText(interestString);

       /* ++++++++++++++++++++++++++++++++++++++++++++++++*/
      }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Calculater().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea amSchedArea;
    private javax.swing.JButton calButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JMenuItem fifYearMort;
    private javax.swing.JTextField interestRateText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea monthlyPaymentArea;
    private javax.swing.JComboBox preMortComboBox;
    private javax.swing.JTextField principalText;
    private javax.swing.JMenuItem rateFromFile;
    private javax.swing.JButton resetButon;
    private javax.swing.JMenuItem seveYearMort;
    private javax.swing.JTextField termText;
    private javax.swing.JMenuItem thirYearMort;
    // End of variables declaration//GEN-END:variables

}



Attached File(s)

  • Attached File  rate.TXT (4bytes)
    Number of downloads: 117


Is This A Good Question/Topic? 0
  • +

Replies To: File Input Stream

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: File Input Stream

Posted 16 September 2010 - 08:46 PM

What does this mean ?
interestString=(strContent;


This post has been edited by guidojavier: 16 September 2010 - 08:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: File Input Stream

Posted 17 September 2010 - 01:56 AM

'interestRateText' needs to be a combo box filled from the rates file
Was This Post Helpful? 0
  • +
  • -

#4 Guest_admum*


Reputation:

Re: File Input Stream

Posted 17 September 2010 - 05:21 AM

View Postguidojavier, on 16 September 2010 - 07:46 PM, said:

What does this mean ?
interestString=(strContent;



This was just a thought and a start. actually means nothing
Was This Post Helpful? 0

#5 Guest_Guest*


Reputation:

Re: File Input Stream

Posted 17 September 2010 - 05:50 AM

View Postg00se, on 17 September 2010 - 12:56 AM, said:

'interestRateText' needs to be a combo box filled from the rates file


Okay
I tried to put the code with in the combo box but it went worse than under the menu bar.

I think my problem was I did not keep the file finder in a separate class.

So I guess my question is how do I take the data in the text file and have it populate the combo box?
also if the information label is "7 years for 5.75%", or where will the rates from the file actually go, to the preMortComboBox.setModel or to an array that would populate the array.

I am thinking to populate the array, so how would I link the two?

So if I enter a array such as double [] rate = {} and point all the if statements to this array, I should have my solution.

Thanks g00se for the idea. This just leaves linking the file finder to the array.
Was This Post Helpful? 0

#6 admum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 10

Re: File Input Stream

Posted 17 September 2010 - 06:06 AM

View PostGuest, on 17 September 2010 - 04:50 AM, said:

View Postg00se, on 17 September 2010 - 12:56 AM, said:

'interestRateText' needs to be a combo box filled from the rates file


Okay
I tried to put the code with in the combo box but it went worse than under the menu bar.

I think my problem was I did not keep the file finder in a separate class.

So I guess my question is how do I take the data in the text file and have it populate the combo box?
also if the information label is "7 years for 5.75%", or where will the rates from the file actually go, to the preMortComboBox.setModel or to an array that would populate the array.

I am thinking to populate the array, so how would I link the two?

So if I enter a array such as double [] rate = {} and point all the if statements to this array, I should have my solution.

Thanks g00se for the idea. This just leaves linking the file finder to the array.


G00se
one thing I forgot, the information in the combo box is a string. I can not get the array to attach to the string. The array wants to be a double.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: File Input Stream

Posted 17 September 2010 - 06:55 AM

The attached should start you off - just reimplement getInterestRatesModel()

Attached File(s)

  • Attached File  Main.txt (34.19K)
    Number of downloads: 107

This post has been edited by g00se: 17 September 2010 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#8 admum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 10

Re: File Input Stream

Posted 17 September 2010 - 06:58 AM

View Postg00se, on 17 September 2010 - 05:55 AM, said:

The attached should start you off - just reimplement getInterestRatesModel()


G00se

I do not see an attachment
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: File Input Stream

Posted 17 September 2010 - 06:59 AM

Sorry - didn't attach properly - see above
Was This Post Helpful? 0
  • +
  • -

#10 admum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 10

Re: File Input Stream

Posted 17 September 2010 - 07:19 AM

View Postg00se, on 17 September 2010 - 05:55 AM, said:

The attached should start you off - just reimplement getInterestRatesModel()


The problem G00se is that takes away the ability to edit the interest rate.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: File Input Stream

Posted 17 September 2010 - 07:35 AM

Well, reading from a file is quite a different kind of functionality. But actually it doesn't - just set the combo to be editable ;)
Was This Post Helpful? 0
  • +
  • -

#12 admum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 10

Re: File Input Stream

Posted 17 September 2010 - 08:34 AM

View Postg00se, on 17 September 2010 - 06:35 AM, said:

Well, reading from a file is quite a different kind of functionality. But actually it doesn't - just set the combo to be editable ;)


Were are you telling it to use the intrestRateText field?

I went to add the ComboBox.setEditable(true) but I do not see the rest of the setModel for the new combo box
Was This Post Helpful? 0
  • +
  • -

#13 admum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 10

Re: File Input Stream

Posted 17 September 2010 - 10:25 AM

g00se

I was never able to get it to work right, so I switched from file input stream to StreamTokenizer.

the problem that I have now is the number in my txt file is 30 but it is being displayed as 30.0 in the ternText field.

Is this an issue with StreamTonenizer or somewhere else in my program?

this is the code for the StreamTokenizer section
private void rateFromFileActionPerformed(java.awt.event.ActionEvent evt) {
   
try
  {
   StreamTokenizer st=new StreamTokenizer(new BufferedReader(new FileReader("rate.txt")));
        int linecount=0;
  while(st.nextToken()!=StreamTokenizer.TT_EOF)
  {
   if(st.lineno() ==LinesGenerated)
   {
   if(st.ttype==StreamTokenizer.TT_NUMBER)
   term =(double) st.nval;
   termText.setText(""+st.nval);
   st.nextToken();
   if(st.ttype==StreamTokenizer.TT_NUMBER)
   rate= st.nval;
   interestRateText.setText(""+st.nval);

  //break;
   }
  // System.out.println (st.lineno())    ;
  }

       }catch (Exception addException){//Catch exception if any
        JOptionPane.showMessageDialog(null, "Error While reading Loandata file\n" +  addException);
     }
       
      }


Was This Post Helpful? 0
  • +
  • -

#14 admum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-September 10

Re: File Input Stream

Posted 17 September 2010 - 10:33 AM

The problem is with
termsYear = Integer.parseInt(
                    this.termText.getText());



I need to rewrite this to work with a double instead of a int
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: File Input Stream

Posted 17 September 2010 - 10:46 AM

Just use a Scanner

    private javax.swing.ComboBoxModel getInterestRatesModel() {
	javax.swing.DefaultComboBoxModel result = null;
	String filename = "rates.txt";
	java.util.Scanner s = null;
	try {
	    result = new javax.swing.DefaultComboBoxModel();
	    s = new java.util.Scanner(new File(filename));
	    while(s.hasNextLine()) {
		result.addElement(s.nextLine());
	    }
	}
	catch(IOException e) {
	    e.printStackTrace();	
	}
	finally {
	    try { s.close() ; } catch(Exception e) { /* ignore */ }	
	}
	return result;
    }



This post has been edited by g00se: 17 September 2010 - 10:47 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2