954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Credit Card Bill Program!

The issues with this program are:

1. File: D:\Unit 3 Data Type, Objects, Assignments\CreditCardBill.java [line: 20]
Error: Type mismatch: cannot convert from double to int

2. File: D:\Unit 3 Data Type, Objects, Assignments\CreditCardBill.java [line: 21]
Error: Type mismatch: cannot convert from double to int

EDIT: I have tried type casting, but I believe you somehow can't go from a double to an int, but instead only the other way is possible. Added a few more variables which I was missing.

Say that you owe the credit card company $1000.00. The company charges you 1.5% per month on the unpaid balance. You have decided to stop using the card and to pay off the debt by making a monthly payment of N dollars a month. Write a program that asks for the monthy payment, then writes out the balance and total payments so far for every succeeding month until the balance is zero or less.

For each month, calculate the interest due on the unpaid balance. Then calculate the new balance by adding the interest and subtracting the payment.

Your output should look like this:

Enter the monthly payment: 100
Month: 1 balance: 915.0 total payments: 100.0
Month: 2 balance: 828.73 total payments: 200.0
Month: 3 balance: 741.16 total payments: 300.0
Month: 4 balance: 652.27 total payments: 400.0
Month: 5 balance: 562.06 total payments: 500.0
Month: 6 balance: 470.49 total payments: 600.0
Month: 7 balance: 377.54 total payments: 700.0
Month: 8 balance: 283.21 total payments: 800.0
Month: 9 balance: 187.46 total payments: 900.0
Month: 10 balance: 90.27 total payments: 1000.0
Month: 11 balance: -8.38 total payments: 1100.0


I have attempted to do this program as follows:

import java.util.Scanner;
class CreditCardBill{public static void main (String[] args){  Scanner input = new Scanner(System.in);  int month = 1;  month++;  System.out.println ("Enter the monthly payment: ");  int pay = input.nextInt();  pay++;  int left = 1000;  double interest = 1.5;  int balance = (left + interest);  int nbalance = (balance + interest) - pay;   while (nbalance >=1) {  System.out.println ("Month: "+ month +   "Balance: " + nbalance +     "Total Payments: " + pay);  }}}

Any help with this program is greatly appreciated. Thanks!

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Edited the program a bit, but still have the same 2 errors as mentioned above.

import java.util.Scanner;

class CreditCardBill{
public static void main (String[] args){
  Scanner input = new Scanner(System.in);
  int month = 1;
  month++;
  System.out.println ("Enter the monthly payment: ");
  int pay = input.nextInt();
  pay++;
  int left = 1000;
  double interest = 1.5;
  int balance = (left + interest);
  int nbalance = (balance + interest) - pay; 
  while (nbalance >=1) {
  System.out.println ("Month: "+ month +   "Balance: " + nbalance +     "Total Payments: " + pay);
  }
}
}
JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

A way to convert from double to int is to use Double.intValue()

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
A way to convert from double to int is to use Double.intValue()

Thanks for helping! Sadly, it didn't work :( haha

In this program, I am trying to add 1.5% to the initial value which is named as LEFT. Since, in Java the % function is used for modular division, that is not possible. Do you know of any other way to do this?

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

If the output should be a double then why not declare balance and nbalance as a double?
and why not declare left as a double too?

and if you want left to be int then just convert it double then

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

If the output should be a double then why not declare balance and nbalance as a double? and why not declare left as a double too?

and if you want left to be int then just convert it double then

Haha, thanks! that fixed the issue of converting...but still leaves adding a percentage a mystery. Are you aware of how to add a percentage to a number? For instance, adding 1.5% to $1000 (In Java of course).

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

then why not convert it say 1.5 percent will become .015 then add it or do any operation with it

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
then why not convert it say 1.5 percent will become .015 then add it or do any operation with it

Thanks again!

So now my program looks like this:

import java.util.Scanner;

class CreditCardBill{
public static void main (String[] args){
  Scanner input = new Scanner(System.in);
  int month = 1;
  System.out.println ("Enter the monthly payment: ");
  int pay = input.nextInt();
  double left = 1000;
  double interest = 0.015;
  double balance = (left * interest) + left;
  double nbalance = (balance - pay); 
  while (nbalance >=1) {
  System.out.println ("Month: "+ month +   " Balance: " + nbalance +     " Total Payments: " + pay);
  month++;
  pay++;
  break;  
  }
}
}


Now, it runs fine, but it only outputs the first line, which is:

Enter the monthly payment:
100
Month: 1 Balance: 915.0 Total Payments: 100

So basically, after the user inputs the monthly payment as 100, all it prints out is that line ^
>

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Of course it will only print out that line...
you don't have a loop

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
Of course it will only print out that line... you don't have a loop
while (nbalance >=1) {


Isn't that a loop? I made that loop so that it can keep running that line and accumulating both month and day. Accumulating month by 1 and pay by 100. Basically, while the variable nbalance is greater than or equal to 1, it should keep printing. I want the program to end when there's no more money left to owe.

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

whoops sorry missed that... should have noted that you are using a break ... to stop the loop
instead you should have the previous operations on the loop that will deduct the money(to make nbalance less than 1)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
whoops sorry missed that... should have noted that you are using a break ... to stop the loop and you have no operation on the loop that will deduct the money

Yes, but when that break is removed, it becomes an infinite loop. The months accumulate, the nbalance and pay don't change at all, instead just keep repeating itself.

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 
Yes, but when that break is removed, it becomes an infinite loop. The months accumulate, the nbalance and pay don't change at all, instead just keep repeating itself.

I edited my previous post and stated that you should have included your previous operation on the loop that will deduct nbalance to make it less than 1 to stop the loop

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Okay I can see the whole picture now...

Include some of your previous operations on the loop and you should be done ;)

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
I edited my previous post and stated that you should have an operation on the loop that will deduct nbalance to make it less than 1 to stop the loop
for (double nbalance = (balance - pay); nbalance>=1; nbalance--) {


I changed my while loop, into a for loop. The issues remaining are:

nbalance decreases by 1 and pay increases by 1

I'm not sure how to make pay increment by 100.

EDIT: I got pay to increment by 100, but nbalance is still decrementing by 1.

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

you don't need to change it to a for loop just add the previous operations on your while loop, make a few adjustments then you should be done

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

The output should look like this:


Enter the monthly payment: 100
Month: 1 balance: 915.0 total payments: 100.0
Month: 2 balance: 828.73 total payments: 200.0
Month: 3 balance: 741.16 total payments: 300.0
Month: 4 balance: 652.27 total payments: 400.0
Month: 5 balance: 562.06 total payments: 500.0
Month: 6 balance: 470.49 total payments: 600.0
Month: 7 balance: 377.54 total payments: 700.0
Month: 8 balance: 283.21 total payments: 800.0
Month: 9 balance: 187.46 total payments: 900.0
Month: 10 balance: 90.27 total payments: 1000.0
Month: 11 balance: -8.38 total payments: 1100.0

but instead I'm getting the balance decrementing by 1, leading month and total payments to increment to around 90000.0 +

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

pay++ only adds 1 to your previous amount e.g 100...101...102

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Try to trace your program and see the logic... your almost done

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
Try to trace your program and see the logic... your almost done


Correct!

Sorry, I forgot to add that I had edited it to:

pay = pay+100;

The issue still lies in nbalance not decrementing accordingly. I need it to multiply by interest and then be subtracted by pay.

nbalance = (nbalance * interest) - 100;


EDIT: When I had tried adding this ^ into the for loop, it only printed out the output line once.

JavaPrograms
Junior Poster in Training
67 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: