-4

I've got to create a code in Java for a homework assignment displaying a salary starting at 1 cent and doubling daily for 10 days. My only problem is that I'm having trouble converting the salary into decimal format and my output is coming out in dollars, not cents. I'm very new to coding and can use any help I can get. My error is that "salary" is already defined in the main, but when i move the decimal format code above the main the problem still exists. How do I move the decimal place over two places? My output is posted under the code so you guys can see my problem. Thank you!!

import java.util.Scanner;
import java.text.DecimalFormat;

public class hw2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//Find number of days worked
int days;
System.out.println("How many days did you work under the current 
salary?");
days = in.nextInt();
int num = 1;
while (num <= days)
 {
    double salary = Math.pow(2, (num-1));
    DecimalFormat salary = new DecimalFormat("##.00");
    System.out.println("Your salary for day " + num + " is $" + 
    salary);
        num++;
        }
   }
}

Output:

How many days did you work under the current salary?

10

Your salary for day 1 is $1.0

Your salary for day 2 is $2.0

Your salary for day 3 is $4.0

Your salary for day 4 is $8.0

Your salary for day 5 is $16.0

Your salary for day 6 is $32.0

Your salary for day 7 is $64.0

Your salary for day 8 is $128.0

Your salary for day 9 is $256.0

Your salary for day 10 is $512.0

Claire C
  • 1
  • 1
  • 1
    Name the `DecimalFormat` variable something else. --- Multiply by 100 to move the decimal point 2 places. – Andreas Aug 31 '17 at 00:39
  • Possible duplicate of [Read/convert an InputStream to a String](https://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string) – Melchia Aug 31 '17 at 00:41
  • Did you read the code you wrote? You clearly can't have `double salary` and `DecimalPoint salary` in the same scope at the same time. It's like George Foreman with the 5 sons named George - how do they tell which one Mrs. Foreman is angry at when she shouts *George! Get your butt in here!*? Except in this case, `double salary` just disappears and gets replaced by `DeclmalPoint salary`. – Ken White Aug 31 '17 at 00:44

2 Answers2

0

You have two variables with the same name and you are not using DecimalFormat to format the salary. You have two options to achieve what you want:

Option #1

Use DecimalFormat to format your double:

double salary = Math.pow(2, (num-1));
DecimalFormat round = new DecimalFormat("##.00");
System.out.println("Your salary for day " + num + " is $" + 
round.format(salary));

Notice that you need to use .format()

Option #2

Use printf() to format your output as the following:

double salary = Math.pow(2, (num-1));
System.out.printf("Your salary for day %d is $%.2f%n", num, salary);

Or use String.format() which will do the same thing:

double salary = Math.pow(2, (num-1));
System.out.println(String.format("Your salary for day %d is $%.2f", num, salary));
Mohd
  • 5,075
  • 7
  • 17
  • 29
0

Your issue is occurring because you already used the variable name "salary" to refer to the variable containing the actual salary. Each variable name can be only used once within the same scope. That is why you received the error.

However, you also need to use the decimal format correctly in order to display the results as you want. Please refer to Double decimal formatting in Java in order to learn how to do this.

Replacing the while block with the following should give the correct solution:

while (num <= days){
    double salary = Math.pow(2, (num-1));
    DecimalFormat salaryFormat = new DecimalFormat("##.00");
    System.out.println("Your salary for day " + num + " is $" + salaryFormat.format(salary));
    num++;
}
Pierce Mason
  • 3
  • 1
  • 2