The code does not create a file
Hi All,
I wrote a program to create a file called "rate1.txt". It did not work. How can I make it work? Thanks a lot.
The code is below
try {
File f = new File("rate.txt");
FileInputStream file = new FileInputStream(f);
File f1 = new File("rate1.txt");
f1.createNewFile();
DataOutputStream s10 = new DataOutputStream(
new FileOutputStream(f1));
s10.writeChar(1);
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
Re: The code does not create a file
Sorry, it does create a file but the file is empty.
Re: The code does not create a file
Have you tried flushing and then closing the output stream after you're finished with it?
Re: The code does not create a file
I changed
s10.writeChar(1);
to
s10.writeChar(33);
The code works. But it works in Intellij Idea 12, not in textpad 5. When I run the same code in textpad 5, it only shows a DOS screen with the usage info and not create the file.
Re: The code does not create a file
Please re-read my post above. And yes, you should write printable characters. Also, if you are writing chars to a file, why not use a Writer instead such as a PrintWriter?
Re: The code does not create a file
The code is part of a java windows application. It should display a window. In Intellij Idea 12, it works. But in textpad 5, the last line in the DOS window is "Press any key to continue...". When I press Enter key, it does not display the window.
Re: The code does not create a file
Quote:
Originally Posted by
yma16
The code is part of a java windows application. It should display a window.
I'm confused as I see nothing in your code above that should cause a window to be displayed. Is there code that you're not showing us?
Re: The code does not create a file
I am new to Java. I am trying different ways to test the code. I am going to use FileWriter next time. Thanks.
Re: The code does not create a file
Quote:
Originally Posted by
yma16
I am new to Java. I am trying different ways to test the code. I am going to use FileWriter next time. Thanks.
That's fine, but you should wrap it in something that does some buffering such as a BufferedWriter that is then wrapped by a PrintWriter.
But back to your main problem -- if your error is in a window not showing and you need our help, you would be wise to show us the code for the window opening as well as describe the problem in greater detail.
Re: The code does not create a file
The application has two java files and one text file. I list them below. Again, it works in Intellij Idea 12, but not in textpad 5.4.2. I do not know what code caused the problem. Please ingore the original question since I fixed it. Thank you all very much.
Java file 1-LoanProgram2.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoanProgram2 extends JFrame implements ActionListener{
double p0=0.0;
Container pane;
JButton compute = new JButton("Compute");
JButton exit = new JButton("Exit");
JLabel l=new JLabel("Selected Term is ");
JLabel a=new JLabel("Enter Loan Amount");
JTextField amt= new JTextField("100000.00");
JLabel pay=new JLabel("");
ButtonGroup group0 = new ButtonGroup( );
JRadioButtonMenuItem year7 = new JRadioButtonMenuItem("7 Years at 5.35%");
JRadioButtonMenuItem year15 = new JRadioButtonMenuItem("15 Years at 5.5%");
JRadioButtonMenuItem year30 = new JRadioButtonMenuItem("30 Years at 5.75%",true);
JTextArea textArea = new JTextArea(10, 50);
public LoanProgram2(){
super("Loan Payment");
setSize(600, 400);
setLocation(200, 50);
JMenuItem quitItem = new JMenuItem("Exit");
compute.addActionListener(this);
exit.addActionListener(this);
quitItem.addActionListener(this);
// create the term menu
JMenu menu1 = new JMenu("Select a Term");
group0.add(year7);
menu1.add(year7);
group0.add(year15);
menu1.add(year15);
group0.add(year30);
menu1.add(year30);
menu1.addSeparator( );
menu1.add(quitItem);
// create a menu bar and use it in this JFrame
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu1);
JScrollPane s = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS);
FlowLayout flo = new FlowLayout(FlowLayout.LEFT,10,20);
pane = getContentPane();
pane.setLayout(flo);
pane.add(l);
pane.add(a);
pane.add(amt);
pane.add(pay);
pane.add(compute);
pane.add(exit);
pane.add(s);
setJMenuBar(menuBar);
}
public void actionPerformed(ActionEvent e){
double loans[][] = new double[2][];
loans[0] = new double[360];//principal in each payment
loans[1] = new double[360];//interest in each payment
double l0=0.0, i0=0.0, t0=0.0;
if (e.getActionCommand().equals("Exit")){
System.exit(0);
}
if (e.getActionCommand().equals("Compute")){
if (isNum(amt.getText())){
if (year7.isSelected()){
i0=5.35/1200.0;
t0=7*12;
l.setText(l.getText() + year7.getText() + " Annual Interest.");
}
if (year15.isSelected()){
i0=5.5/1200.0;
t0=15*12;
l.setText(l.getText() + year15.getText() + " Annual Interest.");
}
if (year30.isSelected()){
i0=5.75/1200.0;
t0=30*12;
l.setText(l.getText() + year30.getText() + " Annual Interest.");
}
l0=Double.parseDouble(amt.getText());
p0=pmt(l0,i0,t0);
pay.setText("Monthly Payment is " + p0 + ".");
double principal=0.0, interest=0.0;
textArea.setText("");
textArea.insert("No. Principal Interest\n",0);
for (int k=1; k<=t0; k++){
principal = pamtsCompute(p0,i0,t0, k);
interest = p0-pamtsCompute(p0, i0, t0,k);
interest = Math.round(100*interest)/100.0;
textArea.append(" "+String.valueOf(k) + " " + String.valueOf(principal) + " " + String.valueOf(interest) + "\n");
}
}}
}
double pmt(double l,double i,double n){
p0=l/((1-Math.pow(1/(1+i),n))/i);
p0= Math.round(100*p0)/100.0;
return p0;
}
double pamtsCompute(double p,double i,double n,double k){//p is monthly payment
double pamt;
pamt= p*Math.pow(1/(1+i),(n-k+1));
pamt= Math.round(100*pamt)/100.0;
return pamt;
}
public boolean isNum(String i)
{
try
{
Double.parseDouble(i);
return true;
}
catch(NumberFormatException nfe)
{
pay.setText("Some of the loan inputs is not a number");
return false;
}
}
public static void main(String[] args){
LoanProgram2 f= new LoanProgram2();
f.setVisible(true);
}}
Java file 2-LoanProgram4.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.geom.*;
public class LoanProgram4 extends LoanProgram2{
JButton compute1 = new JButton("Compute from Input");
JButton draw = new JButton("Draw Pies");
JLabel inter=new JLabel("Annual Interest(%):");
JLabel term=new JLabel("Term(Years):");
JComboBox ii = new JComboBox();
JTextField tt= new JTextField(2);
JLabel no=new JLabel("Payment No:");
JLabel drawMsg=new JLabel("");
JTextField payNo= new JTextField(2);
boolean bDraw = false;
double[] iRate;//will hold rates.
double[] iArray;//will hold interest.
double[] pArray;//will hold principal.
DrawPie p;
public LoanProgram4(){
super();
compute1.addActionListener(this);
pane.add(inter);
pane.add(ii);
pane.add(term);
pane.add(tt);
pane.add(compute1);
pane.add(no);
pane.add(payNo);
ii.setEditable(false);
draw.addActionListener(this);
pane.add(draw);
pane.add(drawMsg);
}
public void actionPerformed(ActionEvent e){
double l0=0.0, i0=0.0;
int t0=0;
l.setText(l.getText()+":");
if (e.getActionCommand().equals("Compute from Input")){
if (isNum(amt.getText()) && isInt(tt.getText())){
i0=Double.parseDouble(ii.getSelectedItem().toStrin g())/1200.0;
t0=Integer.parseInt(tt.getText())*12;
l0=Double.parseDouble(amt.getText());
iArray = new double[t0];
pArray = new double[t0];
l.setText("Loan is " + tt.getText() + " years at Annual Interest " + ii.getSelectedItem().toString() + "%.");
p0=pmt(l0,i0,t0);
p0=pmt(l0,i0,t0);
pay.setText("Monthly Payment is " + p0 + ".");
double principal=0.0, interest=0.0;
textArea.setText("");
textArea.insert("No. Principal Interest\n",0);
for (int k=1; k<=t0; k++){
principal = pamtsCompute(p0,i0,t0, k);
interest = p0-pamtsCompute(p0, i0, t0,k);
interest = Math.round(100*interest)/100.0;
pArray[k-1] = principal;
iArray[k-1] = interest;
textArea.append(" "+String.valueOf(k) + " " + String.valueOf(principal) + " " + String.valueOf(interest) + "\n");
}
double totalPrincipal=0.0, totalInterest=0.0, totalPayment=0.0;
for (int k=1; k<=t0; k++){
totalPrincipal=totalPrincipal+ pArray[k-1];
totalInterest=totalInterest+iArray[k-1];
}
totalPayment = totalPrincipal + totalInterest;
totalPrincipal = Math.round(100*totalPrincipal)/100.0;
totalInterest = Math.round(100*totalInterest)/100.0;
totalPayment = Math.round(100*totalPayment)/100.0;
textArea.append(" \n");
textArea.append("Total Principal Total Interest Total Payment\n");
textArea.append(" "+String.valueOf(totalPrincipal) + " " + String.valueOf(totalInterest) + " " + String.valueOf(totalPayment) + "\n");
}}else
if (e.getActionCommand().equals("Compute")){
if (isNum(amt.getText())){
if (year7.isSelected()){
i0=5.35/1200.0;
t0=7*12;
l.setText("Loan is " + year7.getText() + " Annual Interest.");
}
if (year15.isSelected()){
i0=5.5/1200.0;
t0=15*12;
l.setText("Loan is " + year15.getText() + " Annual Interest.");
}
if (year30.isSelected()){
i0=5.75/1200.0;
t0=30*12;
l.setText("Loan is " + year30.getText() + " Annual Interest.");
}
iArray = new double[t0];
pArray = new double[t0];
l0=Double.parseDouble(amt.getText());
p0=pmt(l0,i0,t0);
pay.setText("Monthly Payment is " + p0 + ".");
double principal=0.0, interest=0.0;
textArea.setText("");
textArea.insert("No. Principal Interest\n",0);
for (int k=1; k<=t0; k++){
principal = pamtsCompute(p0,i0,t0, k);
interest = p0-pamtsCompute(p0, i0, t0,k);
interest = Math.round(100*interest)/100.0;
pArray[k-1] = principal;
iArray[k-1] = interest;
textArea.append(" "+String.valueOf(k) + " " + String.valueOf(principal) + " " + String.valueOf(interest) + "\n");
}
double totalPrincipal=0.0, totalInterest=0.0, totalPayment=0.0;
for (int k=1; k<=t0; k++){
totalPrincipal=totalPrincipal+ pArray[k-1];
totalInterest=totalInterest+iArray[k-1];
}
totalPayment = totalPrincipal + totalInterest;
totalPrincipal = Math.round(100*totalPrincipal)/100.0;
totalInterest = Math.round(100*totalInterest)/100.0;
totalPayment = Math.round(100*totalPayment)/100.0;
textArea.append(" \n");
textArea.append("Total Principal Total Interest Total Payment\n");
textArea.append(" "+String.valueOf(totalPrincipal) + " " + String.valueOf(totalInterest) + " " + String.valueOf(totalPayment) + "\n");
}}
else if(e.getActionCommand().equals("Draw Pies")){
if (textArea.getText().equals("")){
drawMsg.setText("Compute a loan before drawing pies.");
}else{
if (isInt(payNo.getText())){
double p1=0,i1=0,p2=0,i2=0,iTotal=0,pTotal=0;
int m=0;
drawMsg.setText("");
m=Integer.parseInt(payNo.getText());
if (m<1 || m>pArray.length){
drawMsg.setText("Payment number should be between 1 and " + pArray.length + ".");
}else{
for(int k=0;k<m;k++){
p1=p1+pArray[k];
i1=i1+iArray[k];
}
for(int k=0;k<pArray.length;k++){
pTotal=pTotal+pArray[k];
iTotal=iTotal+iArray[k];
}
p2=pTotal-p1;
i2=iTotal-i1;
if (bDraw){//Test if p exists.
pane.remove(p); //Tf p exists, remove it. We need to add a new one.
p=null;
}
p = new DrawPie(p1,i1,p2,i2);
p.setPreferredSize(new Dimension(400,400));
pane.add(p);
bDraw=true;//p exists. set bDraw to true.
}
}else{
drawMsg.setText("Enter an integer as payment number.");
}
}}
else{
super.actionPerformed(e);
}
}
public boolean isInt(String i)
{
try
{
Integer.parseInt(i);
return true;
}
catch(NumberFormatException nfe)
{
pay.setText("The loan term is not an integer");
return false;
}
}
void getRate(){
try {
File f = new File("rate.txt");
FileInputStream file = new FileInputStream(f);
File f1 = new File("rate1.txt");
f1.createNewFile();
DataOutputStream s10 = new DataOutputStream(
new FileOutputStream(f1));
s10.writeChar(33);
byte[] b = new byte[100];
file.read(b);
String s = new String(b);
s=s.trim();
int[] i= new int[s.length()];
int k=1;
i[0]=s.indexOf(",");
while (i[k-1]>0) {//put position of commas into i[].
i[k]=s.indexOf(",",i[k-1]+1);
k++;
}
int[] pos =new int[--k];
pos=i;
String s1="";
double[] rate = new double[++k];//will hold rates.
// double[] rate = new double[k];//will hold rates.
rate[0]=Double.parseDouble(s.substring(0,pos[0]));
k--;//k is number of commas.
for (int j=0;j<k;j++) {
if (j==k-1){//last comma postion.
s1=s.substring(pos[j]+1);
//rate[j]=Double.parseDouble(s.substring(pos[j]+1));
rate[j+1]=Double.parseDouble(s.substring(pos[j]+1));
}else{
s1=s.substring(i[j]+1,i[j+1]);
//rate[j]=Double.parseDouble(s.substring(pos[j]+1,pos[j+1]));
rate[j+1]=Double.parseDouble(s.substring(pos[j]+1,pos[j+1]));
}
iRate=rate;
}
file.close();
} catch (Exception e) {
System.out.println("Error: " + e.toString());
}
}
public static void main(String[] args){
LoanProgram4 f= new LoanProgram4();
f.setSize(650,650);
f.setVisible(true);
f.getRate();
for(int n=0;n<f.iRate.length;n++){
f.ii.addItem(new Double(f.iRate[n]));
}
}}
class DrawPie extends JPanel {
double p1,i1,p2,i2;
DrawPie(double pPaid,double iPaid,double pPay,double iPay){
p1=pPaid;
i1=iPaid;
p2=pPay;
i2=iPay;
}
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D)comp;
Rectangle2D.Double r1 = new Rectangle2D.Double(10,10,40,40);
comp2D.setColor(Color.cyan);
Rectangle2D.Double r2 = new Rectangle2D.Double(10,60,40,40);
Rectangle2D.Double r3 = new Rectangle2D.Double(10,110,40,40);
Rectangle2D.Double r4 = new Rectangle2D.Double(10,160,40,40);
double total = p1+p2+i1+i2;
double ext1=p1/total*360.0,ext2=i1/total*360.0;
double ext3=p2/total*360.0,ext4=i2/total*360.0;
double w=200, h=150;
double x=200, y=50;
comp2D.setColor(Color.blue);
comp2D.fill(r1);
comp2D.drawString("Principal have been paid", 60,30);
Arc2D.Double a1 = new Arc2D.Double(x,y,w,h,0,ext1,Arc2D.Double.PIE);
comp2D.fill(a1);
comp2D.setColor(new Color(50,155,50));
comp2D.fill(r2);
comp2D.drawString("Inerest have been paid", 60,80);
Arc2D.Double a2 = new Arc2D.Double(x,y,w,h,ext1,ext2,Arc2D.Double.PIE);
comp2D.fill(a2);
comp2D.setColor(Color.magenta);
comp2D.fill(r3);
comp2D.drawString("Loan Balance", 60,130);
Arc2D.Double a3 = new Arc2D.Double(x,y,w,h,ext1+ext2,ext3,Arc2D.Double.P IE);
comp2D.fill(a3);
comp2D.setColor(Color.red);
comp2D.fill(r4);
comp2D.drawString("Future Inerest", 60,180);
Arc2D.Double a4 = new Arc2D.Double(x,y,w,h,ext1+ext2+ext3,ext4,Arc2D.Dou ble.PIE);
comp2D.fill(a4);
}
}
The text file is rate.txt. The content is
2.75,2.875,2.99,3.0,3.125,3.25,3.375,3.5,3.75,4.0, 4.25,4.5,4.75,5.0,5.25,5.5,5.75,6.0,6.25,6.5
Re: The code does not create a file
Please edit your post above and place code tags around your code so that it retains its formatting. To do this you'd do something like this:
[code]
//... your code block goes here
[/code]
Note that the top and bottom tags are different, and you must use square brackets and the slash as I demonstrate here. For more, see my link below.
Re: The code does not create a file
I updated the textpad to 6. It works now. Thanks a lot.