I am a first time user to Java and I am stuck and cant figure out what to do.. Please help. My code is

import java.util.Scanner;
public class rshomework2
{
  
  public static void main ( String[] args)  
  {
   Scanner input = new Scanner(System.in);
   sales;
   final double base = 800;
  
   System.out.println("Enter sales total");
     sales = input.nextDouble();
    
    // Calculate the amount of commission.
 commission = rate * sales;
    
 if (sales < 2999.99)
 rate = 0.02; // 2% commission rate
 else if (sales < 4999.99)
 rate = 0.03; // 3% commission rate
 else if (sales < 8999.99)
 rate = 0.04; // 4% commission rate
 else if (sales < 10999.99)
 rate = 0.05; // 5% commission rate
 else
 rate = 0.06; // 6% commission rate

  // Calaulate the amount of Salary.
    Salary = commission + base;
   
  // output values
   System.out.println ( "Gross Salary is $" + Salary);
   
  }
}

when I try to compile it says error line 12 syntax error, insert assignmentOperator Expression to complete expression. Please help.

Edited 5 Years Ago by ~s.o.s~: Added code tags, please learn to use them.

Define your variable.... Most of your variables are not defined.

Ex in line 8 you have simply written

sales;

You should define it ex :

Double sales;

Likewise check all............

now is says variable reate may not have been initialized. I really do not know what I am doing.

import java.util.Scanner;
public class rshomework2
{

public static void main ( String[] args)
{
//read in pay
Scanner input = new Scanner(System.in) ;
double sales;
double commission;
double rate; // Commission rate
double Salary;
final double base = 800;

System.out.println("Enter sales total");
sales = input.nextDouble();

if (sales <= 2999.99)
rate = 0.02; // 2% commission rate
else if (sales <= 4999.99)
rate = 0.03; // 3% commission rate
else if (sales <= 8999.99)
rate = 0.04; // 4% commission rate
else if (sales <= 10999.99)
rate = 0.05; // 5% commission rate
else
rate = 0.06; // 6% commission rate

// Calculate the amount of commission.
commission = rate * sales;

// Calaulate the amount of Salary.
Salary = commission + base;

// output values
System.out.println ( "Gross Salary is $" + Salary);

}
}

So I got it to work. However when it gives me an answer it comes out like 949.9997. How do I make it only give two places after the decimal?

Use Decimal Format Class....

DecimalFormat df = new DecimalFormat("#.##");
df.format(doubleValues) // Return a String

Since it is a double you get it like that. However if you really need to round off to 2 decimal places. What you could is that like the below pseudo code

Define an int
Assign the answer*100 to the int (say X) as this will evade all the decimal there on.
Then define another double and assign the value of the X/100 which will give your required out put.

Edited 5 Years Ago by Zaad: n/a

This article has been dead for over six months. Start a new discussion instead.