printwriter未创建文本文件

huangapple 未分类评论48阅读模式
英文:

printwriter not creating text file

问题

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

public class InterestCal {

    public static void main(String[] args) {

        JFrame f = new JFrame("Interest Computer");

        f.setSize(500, 300);

        JTabbedPane tabObj = new JTabbedPane();

        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(4, 2, 3, 3));

        tabObj.addTab("Simple", null, panel1, "FirstPanel");

        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.YELLOW);
        tabObj.addTab("Compound", null, panel2, "Second Panel");

        JPanel panel3 = new JPanel();
        tabObj.addTab("Continuous", panel3);

        f.add(tabObj);
        panel1.setLayout(new GridLayout(5, 2, 3, 3));

        JLabel principal1 = new JLabel("Principal $");
        JLabel annual1 = new JLabel("Annual % Rate");
        JLabel Years1 = new JLabel("Years");
        JLabel outputFile1 = new JLabel("Output File");
        JTextField principalField1 = new JTextField("");
        JTextField annualField1 = new JTextField("");
        JTextField YearsField1 = new JTextField("");
        JTextField outputFileField1 = new JTextField("");

        panel1.add(principal1);
        panel1.add(principalField1);

        panel1.add(annual1);
        panel1.add(annualField1);

        panel1.add(Years1);
        panel1.add(YearsField1);

        panel1.add(outputFile1);
        panel1.add(outputFileField1);

        JButton button1 = new JButton("Compute Simple Interest");
        panel1.add(button1);
        button1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                int principalInPanel1 = Integer.parseInt(principalField1.getText());
                double interestRateinPanel1 = Double.parseDouble(annualField1.getText());
                int yearsInPanel1 = Integer.parseInt(YearsField1.getText());

                File filename = new File(outputFileField1.getText());
                PrintWriter writer = null;
                try {
                    writer = new PrintWriter(filename);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }

                for (int x = 0; x < yearsInPanel1 + 1; x++) {

                    double SimpleInterest = principalInPanel1 * (Math.pow(1 + interestRateinPanel1, x));

                    writer.format("%d %.2f\n", x, SimpleInterest);
                    System.out.printf(String.format("%d %.2f\n", x, SimpleInterest));
                }
                writer.close();
                System.exit(0);
            }

        });

        panel2.setLayout(new GridLayout(5, 2, 3, 3));
        JLabel principal2 = new JLabel("Principal $");
        JLabel annual2 = new JLabel("Annual % Rate");
        JLabel Years2 = new JLabel("Years");
        JLabel outputFile2 = new JLabel("Output File");
        JTextField principalField2 = new JTextField("");
        JTextField annualField2 = new JTextField("");
        JTextField YearsField2 = new JTextField("");
        JTextField outputFileField2 = new JTextField("");

        panel2.add(principal2);
        panel2.add(principalField2);

        panel2.add(annual2);
        panel2.add(annualField2);

        panel2.add(Years2);
        panel2.add(YearsField2);

        panel2.add(outputFile2);
        panel2.add(outputFileField2);

        panel3.setLayout(new GridLayout(5, 2, 3, 3));
        JLabel principal3 = new JLabel("Principal $");
        JLabel annual3 = new JLabel("Annual % Rate");
        JLabel Years3 = new JLabel("Years");
        JLabel outputFile3 = new JLabel("Output File");
        JTextField principalField3 = new JTextField("");
        JTextField annualField3 = new JTextField("");
        JTextField YearsField3 = new JTextField("");
        JTextField outputFileField3 = new JTextField("");

        panel3.add(principal3);
        panel3.add(principalField3);

        panel3.add(annual3);
        panel3.add(annualField3);

        panel3.add(Years3);
        panel3.add(YearsField3);

        panel3.add(outputFile3);
        panel3.add(outputFileField3);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}
英文:

I'm having trouble figuring out why the printwriter wont create a file when you type it in the output file textfield i cant move on until i figure out whats the problem. this program is a gui which calculates interest and outputs it into a text file but i cant seem to get that part to work i'd really appreciate it if someone could take a look at it theres only one printwriter in the whole program so far i intend on adding more once i get past this obstacle. im not sure whats wrong with the print writer.

 import java.awt.Color;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.PrintWriter;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JTabbedPane;
 import javax.swing.JTextField;

public class InterestCal {

  public static void main(String[] args)
    {
    
        JFrame f = new JFrame(&quot;Interest Computer&quot;);
        
        f.setSize(500, 300);

        JTabbedPane tabObj = new JTabbedPane();
        
        
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(4,2,3,3));

        tabObj.addTab(&quot;Simple&quot;,null,panel1,&quot;FirstPanel&quot;);

        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.YELLOW);
        tabObj.addTab(&quot;Compound&quot;,null,panel2,&quot;Second Panel&quot;);
        
        JPanel panel3 = new JPanel();
        tabObj.addTab(&quot;Continuous&quot;,panel3);
        
        //add the tabs to the panel
        f.add(tabObj);
        panel1.setLayout(new GridLayout(5,2,3,3));

        //PANEL NUMBER 1
        JLabel principal1 = new JLabel(&quot;Principal $&quot;);
        JLabel annual1 = new JLabel(&quot;Annual % Rate&quot;);
        JLabel Years1 = new JLabel(&quot;Years&quot;);
        JLabel outputFile1 = new JLabel(&quot;Output File&quot;);
        JTextField principalField1  = new JTextField (&quot;&quot;);
        JTextField annualField1     = new JTextField (&quot;&quot;);
        JTextField YearsField1      = new JTextField (&quot;&quot;);
        JTextField outputFileField1 = new JTextField (&quot;&quot;);
        
        //PANEL NUMBER 1
        //adds label then adds text-field 
        panel1.add(principal1);
        panel1.add(principalField1); 

        panel1.add(annual1);
        panel1.add(annualField1);    

        panel1.add(Years1);
        panel1.add(YearsField1);     

        panel1.add(outputFile1);
        panel1.add(outputFileField1);

        
        JButton button1 = new JButton(&quot;Compute Simple Interest&quot;);
        panel1.add(button1);
        button1.addActionListener(new ActionListener()
        
                {
    
                    
            public void actionPerformed(ActionEvent e) {
                
    int principalInPanel1 = 	  Integer.parseInt(principalField1.getText());
    double interestRateinPanel1 = Double.parseDouble(annualField1.getText());
    int yearsInPanel1 =			  Integer.parseInt(YearsField1.getText());
                
    
    
    File filename =new File(outputFileField1.getText());
    //added this so i can use it outside the try/catch block
    PrintWriter writer = null; 
    try {
        writer = new PrintWriter(filename);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }
    
    
    
    for (int x = 0; x &lt; yearsInPanel1+1; x++) {
    
    double SimpleInterest = principalInPanel1*(Math.pow(1+interestRateinPanel1,x));
    
    
    writer.format(&quot;%d %.2f\n&quot;,x,SimpleInterest);
    System.out.printf(String.format(&quot;%d %.2f\n&quot;,x,SimpleInterest));
    }
            writer.close();
            System.exit(0);
            }
        
                    
        });
        
        //PANEL NUMBER 2
        panel2.setLayout(new GridLayout(5,2,3,3));
        JLabel principal2 = new JLabel(&quot;Principal $&quot;);
        JLabel annual2 = new JLabel(&quot;Annual % Rate&quot;);
        JLabel Years2 = new JLabel(&quot;Years&quot;);
        JLabel outputFile2 = new JLabel(&quot;Output File&quot;);
        JTextField principalField2  = new JTextField (&quot;&quot;);
        JTextField annualField2     = new JTextField (&quot;&quot;);
        JTextField YearsField2      = new JTextField (&quot;&quot;);
        JTextField outputFileField2 = new JTextField (&quot;&quot;);
        
        //PANEL NUMBER 2
        //adds label then adds text-field 
        panel2.add(principal2);
        panel2.add(principalField2); 
             
        panel2.add(annual2);
        panel2.add(annualField2);    
             
        panel2.add(Years2);
        panel2.add(YearsField2);     
             
        panel2.add(outputFile2);
        panel2.add(outputFileField2);
        
        //PANEL NUMBER 3
        panel3.setLayout(new GridLayout(5,2,3,3));
        JLabel principal3 = new JLabel(&quot;Principal $&quot;);
        JLabel annual3 = new JLabel(&quot;Annual % Rate&quot;);
        JLabel Years3 = new JLabel(&quot;Years&quot;);
        JLabel outputFile3 = new JLabel(&quot;Output File&quot;);
        JTextField principalField3  = new JTextField (&quot;&quot;);
        JTextField annualField3     = new JTextField (&quot;&quot;);
        JTextField YearsField3      = new JTextField (&quot;&quot;);
        JTextField outputFileField3 = new JTextField (&quot;&quot;);
        
        //PANEL NUMBER 3
        //adds label then adds text-field 
        panel3.add(principal3);
        panel3.add(principalField3); 
                 
        panel3.add(annual3);
        panel3.add(annualField3);    
                 
        panel3.add(Years3);
        panel3.add(YearsField3);     
                 
        panel3.add(outputFile3);
        panel3.add(outputFileField3);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

huangapple
  • 本文由 发表于 2020年4月6日 15:06:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/61054580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定