Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 reading and writing to text file
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mendem03




PostPosted: Fri Jun 04, 2010 9:32 am   Post subject: reading and writing to text file

Java:

import java.io.*;
class finalproject_InterestRate
{
                public static void main(String[]text)
        {
                try{
                        double amount;
                        double interestRate[]=new double[3];   
                        DataInput d = new DataInputStream(System.in);
                        String txt;
                       
                       
                       
                        System.out.println("\n" + "We are offering loans upto $50000 at good rates so if anyone is looking for a loan we are here for you.");
                        System.out.println("\n" + "How much money do you want to borrow?.");
                        amount= Double.parseDouble(d.readLine());
                        if (amount<=10000)
                        {
                                System.out.print("\n" + "The interest rate you are borrowing at is ");
                                System.out.print(interestRate[0]=5);
                                System.out.println(" %.");
                                interestRate[0]/=100;
                                interestRate[0]+=1;
                                System.out.println(interestRate[0]);
                        }
                        else if ((amount>10000) && (amount<=20000))
                        {
                                System.out.print("\n" + "The interest rate you are borrowing at is ");
                                System.out.print(interestRate[1]=10);
                                System.out.println(" %.");
                                interestRate[1] /= 100;
                                interestRate[1] += 1;   
                                System.out.println(interestRate[1]);
                        }
                        else if ((amount>20000) && (amount<=50000))
                        {
                                System.out.print("\n" + "The interest rate you are borrowing at is ");
                                System.out.print(interestRate[2]=15);
                                System.out.println(" %.");
                                interestRate[2]/=100;
                                interestRate[2]+=1;
                                System.out.println(interestRate[2]);
                        }
                        else
                                System.out.println("Too much money");
                       
                       
                       
                       
                        System.out.println("For how long would you like the loan for.");
                        System.out.println("Your options are 1 year, 2 years, 5 years, or 10 years.");
                        int time= Integer.parseInt(d.readLine());
                        if (time==1)
                        {
                                System.out.print("\n" + "You will have to pay interest for 1 year, compounded monthly, for ");
                                System.out.print(time*=12);
                                System.out.println(" periods.");
                        }
                        else if (time==2)
                        {
                                System.out.print("\n" + "You will have to pay interest for 2 years, compounded quartly, for ");
                                System.out.print(time*=3);
                                System.out.println(" periods.");
                        }
                        else if (time==5)
                        {
                                System.out.print("\n" + "You will have to pay interest for 5 years compounded Semi annually, for ");
                                System.out.print(time*=2);
                                System.out.println(" periods.");
                        }
                        else if (time==10)
                        {
                                System.out.print("\n" + "You will have to pay interest for 10 years, annually, for ");
                                System.out.print(time*=1);
                                System.out.println(" periods.");
                        }
                        else
                                System.out.println("We dont offer that year.");
                       
                       
                        double amountPayment;
                       
                       
                        System.out.println("\n" + "How much money will you pay each period?");
                        amountPayment= Double.parseDouble(d.readLine());
                        System.out.println("I would like to pay $ " + amountPayment + "\n");
                        double totalPayment;
                        double interestPayment=0;
                        int period=1;
                        if (amount<=10000)
                        {
                                for(int interestPeriod=1; interestPeriod<=time; interestPeriod++)
                                {       
                                        System.out.println("\nYour interest payment is $" + (interestPayment=((amount*interestRate[0])-amount)));
                                        System.out.print("Your total payment is $");
                                        System.out.println(totalPayment=interestPayment+amountPayment);
                                        System.out.print("You still have to pay $");
                                        System.out.println(amount-=amountPayment);     
                                }
                        }
                        if (amount>10000 && amount<=20000)
                        {
                                for(int interestPeriod=1; interestPeriod<=time; interestPeriod++)
                                {
                                       
                                        System.out.println("\nYour interest payment is $" + (interestPayment=((amount*interestRate[1])-amount)));
                                        System.out.print("Your total payment is $");
                                        System.out.println(totalPayment=interestPayment+amountPayment);
                                        System.out.print("You still have to pay $");
                                        System.out.println(amount-=amountPayment);
                                }
                        }
                        if (amount>20000 && amount<=50000)
                        {
                                for(int interestPeriod=1; interestPeriod<=time; interestPeriod++)
                                {
                                       
                                        System.out.println("\nYour interest payment is $" + (interestPayment=((amount*interestRate[2])-amount)));
                                        System.out.print("Your total payment is $");
                                        System.out.println(totalPayment=interestPayment+amountPayment);
                                        System.out.print("You still have to pay $");
                                        System.out.println(amount-=amountPayment);
                                }
                        }
                }catch (IOException ignored){}
        }
}


Need help for reading and writing this program to a text file.
Can someone please help me.
Sponsor
Sponsor
Sponsor
sponsor
Unnamed.t




PostPosted: Fri Jun 04, 2010 5:38 pm   Post subject: Re: reading and writing to text file

INPUT:: You are currently using a DataInput object in coordination with a System.in input stream.
OUTPUT:: You are currently using System.out

What you need to do is one of the following:

INPUT

Use the scanning method from java.util.Scanner class

Java:
Scanner in= new Scanner (new File ("Filename.txt"));


Check the different types of inputs that can be performed by using Help Reference of the Java class libraries (can be found on the sun microsystems java site)

OR

Use the BufferedReader method from java.io.BufferedReader class

Java:
BufferedReader in = new BufferedReader (new FileReader ("Filename.txt"));


Check the different types of inputs that can be performed by using Help Reference of the Java class libraries (can be found on the sun microsystems java site)

OUTPUT

Use the PrintWriter method from java.io.PrintWriter class

Java:
PrintWriter out = new PrintWriter (new FileWriter ("Filename.txt"));


This can be used to output text to your file. Once again check java class libraries for methods e.g. out.println ();/out.print ();

OR

Use the BufferedWriter method from java.io.BufferedWriter class

Java:
BufferedWriter out = new BufferedWriter (new FileWriter ("Filename.txt"));


Check the different types of inputs that can be performed by using Help Reference of the Java class libraries (can be found on the sun microsystems java site)

Website for class libraries: http://java.sun.com/j2se/1.5.0/docs/api/
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: