My program works, but there is one slight thing that is off about it and I canno
ID: 3630725 • Letter: M
Question
My program works, but there is one slight thing that is off about it and I cannot figure out how to fix it. Basically It asks the user about the number of items a person wants to purchase with a credit card. it takes in the user input about the item description, interest rate, principle, and minimum monthly payment and outputs how long it'll take to pay the loan off. Here is my code:/*start*/
import java.util.*;
public class Loan
{
public static void main(String [] args)
{
int a,b;
Scanner console = new Scanner(System.in);
System.out.println("Welcome to interest calculator");
System.out.println(" Enter the number of items you want to purchase using your credit card");
int amount = console.nextInt();
console.nextLine();
for(a=1;a<=amount;a++)
{
System.out.print("Enter the description for the item #");
System.out.println(a);
String userinput = console.nextLine();
System.out.print("Enter the price of the ");
System.out.println(userinput);
double principal = console.nextDouble();
System.out.println("Enter the interest rate:");
double rate = console.nextDouble();
System.out.println("Enter the minimum monthly payment:");
double payment = console.nextDouble();
System.out.print(userinput +" ");
System.out.print(principal +" ");
System.out.print(rate +" ");
System.out.println(payment +" ");
System.out.println("0.015*****");
rate= (rate/100)/12;
getTable(principal,payment,rate);
console.nextLine();
System.out.print(" ");
}
System.out.println("Thanks for using interest calculator");
System.out.print(" ");
System.out.println("Hit enter to use Future Price software");
String enter = console.nextLine();
console.nextLine();
System.out.print(" ");
}
/*method for first part*/
public static double getInterest(double principal, double rate)
{
return principal*rate;
}
public static int getTable(double principal, double payment, double rate)
{
int count;
double totint=0;
double interest;
for(count=1;principal>0;count++)
{
interest=getInterest( principal,rate);
totint+=interest;
System.out.print("$");
System.out.printf("%.2f", interest);
System.out.println(" will go towards interest.");
System.out.print("$");
System.out.printf("%.2f", (payment-interest));
System.out.println(" will go towards interest.");
principal=principal+interest-payment;
System.out.print("You still have $");
System.out.printf("%.2f", principal);
System.out.println(" to pay.");
System.out.println("***********************************");
}
interest=principal*rate;
totint+=interest;
System.out.print("It will take you ");
System.out.print(count);
System.out.println(" months to pay off the TV");
System.out.print("You will have paid ");
System.out.printf("%.2f", totint);
System.out.println(" in interest.");
return count;
}
}
/* end of program */
My problem is at the very end. Regardless of what i write, the very last two lines will always say "It will take you n months to pay off the TV"
I DO NOT want the TV there. I want whatever the user inputted as the description to be there and for it be changed every time the user inputs a different description for each item description the user inputted. How would i do this? will i have to make major changes to my code?
Basically I'd like it to run like this
/*start*/
Enter the number of items you want to purchase using your credit card
/*user input, lets say its 3*/
Enter the description for the item #1
/*user input, lets say its a TV*/
/*skip to end after all input is taken and calculations are done*/
It will take you n months to pay off the [user input for description]
you will have paid n in interest.
Enter the description for the item #2
/*user input, lets say its a PS3*/
/*skip to end*/
It will take you n months to pay off the [user input for description of item #2]
/*repeat for item #3*/
/*end program*/
How would i do this? I do know in my programming i have hard coded it to always say TV, but i'm wondering how i can change that to allow user input to dictate what the output says .
Explanation / Answer
please change that line to.
System.out.println(" months to pay off the " + userinput);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.