2011-07-22 19 views
0

シーケンシャルファイルの読み取り可能な重複:
How do I populate jcombobox from a textfile?Javaの住宅ローンの計算は

OK、私は静かな数字出ていないことを、ここで問題が発生しています。私は、各行に変数を持つファイルを持っています:apr.txtというファイルの5.35,5.5,5.75。私はファイルを読んでいるので、ファイルをJComboBoxに読み込む必要があります。これまでのところ、ファイルは正常に読み込まれていますが、JComboBoxに読み込む方法はわかりません。これは何か簡単でなければなりません。誰かが正しい方向に向けるのを助けることができますか?

ありがとうございます。

import java.awt.*; 
import java.text.NumberFormat; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.util.StringTokenizer; 

import javax.swing.*; 
import javax.swing.JComboBox; 



    public class MortgageCalculatorGUI9 extends JFrame implements ActionListener 
     { 
     // Declarations 

     //double [] Interest = {5.35, 5.5, 5.75}; 
     int [] Length = {7, 15, 30}; 
     double [] file_data = new double[3]; 


     JLabel mortgagelabel = new JLabel("Principal Amount:$ "); 
     JLabel paymentLabel = new JLabel("Monthly Payment: "); 
     JLabel intRateLabel = new JLabel("Interest Rate %: "); 
     JLabel termLabel = new JLabel("Length of Loan of Loan in Years: " ); 
     JTextField mortgagePrincipal = new JTextField(7); 
     JTextField Payment = new JTextField(7); 
     //JTextField intRateText = new JTextField(3); 
     JComboBox intRateBox = new JComboBox(); 
     JTextField termText = new JTextField(3); 
     JButton b7_535 = new JButton("7 years at 5.35%"); 
     JButton b15_55 = new JButton("15 years at 5.50%"); 
     JButton b30_575 = new JButton("30 years at 5.75%"); 
     JButton exitButton = new JButton("Exit"); 
     JButton clearButton = new JButton("Clear All"); 
     JButton calculateButton = new JButton("Calculate Loan"); 
     JTextArea LoanPayments = new JTextArea(20,50); 


     JScrollPane scroll = new JScrollPane(LoanPayments); 

     public MortgageCalculatorGUI9() 
     { 
      //GUI setup 
      super("Mortgage Calculator 1.0.5"); 
      setSize(800, 400); 
      setLocation(500, 200); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      JPanel pane = new JPanel(new GridLayout(3,1)); Container grid = getContentPane(); 
      grid.setLayout(new GridLayout(4,0,4,4)); pane.add(grid); 
      pane.add(scroll); 
      grid.add(mortgagelabel); 
      grid.add(mortgagePrincipal); 
      grid.add(intRateLabel); 
      //grid.add(intRateText); 
      grid.add (intRateBox); 
      grid.add(termLabel); 
      grid.add(termText); 
      grid.add(paymentLabel); 
      grid.add(Payment); 
      grid.add(b7_535); 
      grid.add(b15_55); 
      grid.add(b30_575); 
      grid.add(calculateButton); 
      grid.add(clearButton); 
      grid.add(exitButton); 
      Payment.setEditable(false); 
      setContentPane(pane); 
      setContentPane(pane); 
      setVisible(true); 

      //add GUI functionality 
      calculateButton.addActionListener (this); 
      exitButton.addActionListener(this); 
      clearButton.addActionListener(this); 
      b7_535.addActionListener(this); 
      b15_55.addActionListener(this); 
      b30_575.addActionListener(this); 
      mortgagePrincipal.addActionListener(this); 
      //intRateText.addActionListener(this); 
      intRateBox.addActionListener (this); 
      termText.addActionListener(this); 
      Payment.addActionListener(this); 

     } 

      public void actionPerformed(ActionEvent e) 
       { 
        Object command = e.getSource(); 

        if (command == exitButton) 
       { 

        System.exit(0); 

       } 
       else if (command == b7_535) 
       { 

        calcLoan(Length[0], file_data[0]); 

       } 
       else if (command == b15_55) 
       { 

        calcLoan(Length[1], file_data[1]); 

       } 
       else if (command == b30_575) 
       { 

        calcLoan(Length[2], file_data[2]); 

       } 

       else if (command == calculateButton) 
       { 

        double terms = 0; 
        double rates = 0; 
        try 

       { 
        terms = Double.parseDouble(termText.getText()); 
        //rates = Double.parseDouble(intRateText.getText()); 
        read_File(); 
       } 
        catch (Exception ex) 
       { 
        LoanPayments.setText("Invalid term or rate Amount"); 
        return; 
       } 

        calcLoan(terms, rates); 

       } 

       else if (command == clearButton) 
       { 

        mortgagePrincipal.setText(""); 
        Payment.setText(""); 
        //intRateText.setText(""); 
        termText.setText(""); 
        LoanPayments.setText(""); 

       } 

      } 


      //Input File 

       public void read_File() 
       { 

        File inFile = new File("apr.txt"); 

        try 
        { 

         BufferedReader istream = new BufferedReader(new FileReader(inFile)); 

         for(int x=0;x<6;x++) 
         { 
          file_data[x]=Double.parseDouble (istream.readLine()); 
         } 



        } 

        catch (Exception ex) 
         { 

          LoanPayments.setText ("Could Not Read From File."); 
          return; 

         } 
       } 



       //this is what needs to be done 
       private void calcLoan(double terms, double rates) 

       { 

        termText.setText(String.valueOf(terms)); 
        //intRateText.setText(String.valueOf(rates)); 
        double amount = 0; 

       try 
       { 

        amount = Double.parseDouble(mortgagePrincipal.getText()); 

       } 

        catch (Exception ex) 

       { 

        LoanPayments.setText("Invalid mortgage Amount"); 
        return; 

       } 

        double interestRate = rates; 

        // Calculations 
        double intRate = (interestRate/100)/12; 
        int Months = (int)terms * 12; 
        double rate = (intRate/12); 
        double payment = amount * intRate/(1 - (Math.pow(1/(1 + intRate), Months))); 
        double remainingPrincipal = amount; 
        double MonthlyInterest = 0; 
        double MonthlyAmt = 0; 
        double[] balanceArray = new double[Months]; 
        double[] interestArray = new double[Months]; 
        double[] monthArray = new double[Months]; 
        NumberFormat Money = NumberFormat.getCurrencyInstance(); 
        Payment.setText(Money.format(payment)); 
        LoanPayments.setText("Month\tPrincipal\tInterest\tEnding Balance\n"); 
        int currentMonth = 0; 

         while(currentMonth < Months) 
          { 

          //create loop calculations 
          MonthlyInterest = (remainingPrincipal * intRate); 
          MonthlyAmt = (payment - MonthlyInterest); 
          remainingPrincipal = (remainingPrincipal - MonthlyAmt); 
          LoanPayments.append((++currentMonth) + "\t" + Money.format(MonthlyAmt) + "\t" + Money.format(MonthlyInterest) + "\t" + Money.format(remainingPrincipal) + "\n"); 
          balanceArray[currentMonth] = MonthlyAmt; 
          interestArray[currentMonth] = MonthlyInterest; 
          monthArray[currentMonth] = currentMonth; 

          } 

         } 


      public static void main(String[] args) 
      { 

      MortgageCalculatorGUI9 frame= new MortgageCalculatorGUI9(); 

      } 
} 
+1

私は 'populate jcombobox fromファイルとボイラー、[答えられた質問](http://stackoverflow.com/questions/3173149/how-do-i-populate-jcombobox-from-a-textfile) - 検索能力の欠如は、なぜ私たちは文句を言う。 –

答えて

1

適切な方法についてはJComboBox APIを見ましたか?あなたがそこから始めれば、StackOverflowで尋ねるよりも早く正しい答えを得ることができるでしょう( "add ... and ends with ... Item");)

+0

実際、ここには自己援助の欠如があるようです。あなたが最初の試みで最初に失敗するならば、SOは、行く場所ではないと考えられます。 –

+0

私はJavaプログラミングを初めて勉強していて、この問題を一日中研究しています。私はこれまでずっと、見た目の良いアプリケーションを作るのに大いに助けなければなりません。私の仕事をしているプログラマーたちも私の試練を受けていると信じています。私はどこから始める必要があり、どこですべてのリソースが私の意志の終わりまでにまだあるか分からない。私は彼らが私に与える援助のために誰にも感謝し、私はそれが貴重なことを知っています。私は何が示唆されているか試し、私がどれくらい得るかを見てみましょう。どうもありがとうございました。 – David

+0

APIを使用しているときは、コードの1行を書く前に、常にAPIのドキュメントを参照するように提案されています。 – buch11

関連する問題