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

Username:   Password: 
 RegisterRegister   
 array help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mendem03




PostPosted: Wed Jun 02, 2010 12:15 pm   Post subject: array help

Java:

import java.io.*;
class like
{
        public static void main(String[]text)
        {
                double amount;
                double interestRate[]=new double[3];   
                DataInput d = new DataInputStream(System.in);
                String txt;
                try{
                        txt=d.readLine();
                       
                        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(" %.");
                                System.out.println((interestRate[0]/=100)+1);
                        }
                        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(" %.");
                                System.out.println((interestRate[1]/=100)+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(" %.");
                                System.out.println((interestRate[2]/=100)+1);
                        }
                        else
                                System.out.println("Too much money");
                       
                       
                       
                        int time1[]=new int[11];
                        int time=1;
                        time1[time]=time;
                       
                        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.");
                        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(time1[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(time1[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(time1[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(time1[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;
                       
                       
                       
                       
                       
                        for(int interestPeriod=1; interestPeriod<=time1[time]; interestPeriod++)
                        {
                                System.out.println("\nYour interest payment is $" + (interestPayment=amount*interestRate[]));
                                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){}
        }
}
 


How do i get the arry value into the loop when im multiplying. interestRate is the array, i want to multiply with the amount.
I really need help with this.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Jun 02, 2010 1:31 pm   Post subject: RE:array help

Perhaps you should index it with interestPeriod, instead of a hard-coded number (1,2,3) like you have above. For example:

Java:

System.out.println( "Your interest payment is $" + (interestPayment=amount*interestRate[interestPeriod]));
mendem03




PostPosted: Wed Jun 02, 2010 3:35 pm   Post subject: Re: array help

i made an adjustment and i need help with this one
Java:

import java.io.*;
class lkj
{
                public static void main(String[]text)
        {
                double amount;
                int interestRate[]=new int[3]
                DataInput d = new DataInputStream(System.in);
                String txt;
                try{
                        txt=d.readLine();
                       
                        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(" %.");
                        }
                        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(" %.");
                        }
                        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(" %.");
                        }
                        else
                                System.out.println("Too much money");
                       
                       
                       
                       
                        int time=1;
                       
                       
                        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.");
                        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;
                        int interestRate2=1;
                        interestRate[interestRate2]=interestRate2;
                       
                        for(int interestPeriod=1; interestPeriod<=time; interestPeriod++)
                        {
                               
                                System.out.println("\nYour interest payment is $" + (interestPayment=amount*((interestRate[interestRate2]/100)+1)));
                                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){}
        }
}


I gave interestRate values to the user and depending on want they pick they will get the value but now im trying to use that value for a calculation but i dont know how to get it to work. Im trying to do the calculation in the loop.
Can someone try the program and help me please.
yoursecretninja




PostPosted: Thu Jun 03, 2010 4:43 pm   Post subject: Re: array help

I just peaked at the stuff pertaining to the interest rate. Why are you using an array for interest rates?

You ask the user to choose an amount of money to borrow. The entered amount of money is compared against three ranges of values.

For amount within range A you set the value of interestRate at index 0.
For amount within range B you set the value of interestRate at index 1.
For amount within range C you set the value of interestRate at index 2.

moving along to the part just before you perform your calculations in that for loop...

You create an integer called interestRate2 and assign it the value of one.

You then assign the value of InterestRate at index interestRate2 to the value of interestRate2... do you see what you did there?

You then calculate the interest payment as amount * ((the value of interestRate at index interestRate2 / 100) + 1).

Does following this logic in plain english, rather than looking at code, help you see why you'll get the wrong answer every time you run the program (assuming the program actually compiles and runs)? Do you see what you are doing when you operate on that array? Do you see how this problem would be much easier if you used an integer to hold the interest rate instead of an array? I hope this is helpful... I'd like to help you understand how to fix this rather than just fixing it for you as this looks like it might be a homework assignment.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: