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!

Recommended Answers

All 65 Replies

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);
  }
}
}

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

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?

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

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).

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

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 ^
>

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

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.

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)

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.

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

Okay I can see the whole picture now...

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

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.

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

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 +

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

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

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.

in your code your multiplying it to balance and not to nbalance directly...

try to change your operations that is suited for the program

can you post your current code

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: ");
  double pay = input.nextInt();
  double left = 1000;
  double interest = 0.015;
  double balance = (left * interest) + left; 
  for (double nbalance = (balance - pay); nbalance>=1; nbalance--) {
  System.out.println ("Month: "+ month +   " Balance: " + nbalance +     " Total Payments: " + pay);
  month++;
  nbalance = (nbalance * interest) - 100;
  pay = pay+100;
  }
}
}

If you read my one of my previous post I told you that use the previous while loop in your code.. there's no need for a for loop or do want to make it complicated?

If you read my one of my previous post I told you that use the previous while loop in your code.. there's no need for a for loop or do want to make it complicated?

I wasn't sure how to change the while loop, and the for loop made a little more sense to me.

nbalance = (nbalance * interest) - 100;

if pay is user input then subtract pay to it

nbalance = (nbalance * interest) - 100;

if pay is user input then subtract pay to it

Ok, changed it to that, and also back to the while loop. So now it 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: ");
  double 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++;
  nbalance = (nbalance * interest) - pay;
  nbalance--;
  pay = pay+100;
  }
}
}

I wasn't sure how to change the while loop, and the for loop made a little more sense to me.

well suit yourself
nbalance-- subracts itself by 1 which makes the output wrong

Trust me its better to use a while loop

Ok, changed it to that, and also back to the while loop. So now it 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: ");
  double 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++;
  nbalance = (nbalance * interest) - pay;
  nbalance--;
  pay = pay+100;
  }
}
}

So does it still have an error?

well suit yourself
nbalance-- subracts itself by 1 which makes the output wrong

Trust me its better to use a while loop

Haha, I trust you, already changed it, you must've missed it. Posted right before your reply! I took out nbalance--, but now it ends up only printing out one of the input lines.

OUTPUT:

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

while (nbalance>=1) {
  System.out.println ("Month: "+ month +   " Balance: " + nbalance +     " Total Payments: " + pay);
  month++;
  nbalance = (nbalance * interest) - pay;
  nbalance--;
  pay = pay+100;
  }

why should it have nbalance--?
also change the position of pay and nbalance operation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.