Hi all,
Having another problem with a program. I thought i had this one pretty good, i was confident about it, but no luck. The problem was with initializing a variable, i try to add in to output what the variable is and it says it needs to might not have been initialized. Im not sure if the problem is that i tried to initialize it inside an if statement or if i have this overloaded and its too much for it to handle. I would much appreciate any help on this one. Here is my code:

import javax.swing.JOptionPane;
public class ComputeNet
{
    public static void main(String[] args)
    {
    String rateString;
    String hourString;
    double rate;
    double hours;
    double grossPay;
    double withholding;
    double netPay;
    int withholding1;
    rateString = JOptionPane.showInputDialog(null,
        "How much do you make per hour?");
    hourString = JOptionPane.showInputDialog(null,
        "How many hours did you work this week?");
    rate = Double.parseDouble(rateString);
    hours = Double.parseDouble(hourString);
    grossPay = rate * hours;
    if(grossPay <= 300)
    {
        withholding = .10;
        netPay = grossPay - (grossPay * withholding);
        withholding1 = 10;
    }
   else 
    {
    if(grossPay >= 300.01)
    {
        withholding = .12;
        netPay = grossPay - (grossPay * withholding);
        withholding1 = 12;
    }
    JOptionPane.showMessageDialog(null,
        "Your gross pay is " + grossPay +
        ", Your withholding is " + withholding1 +
        ", and your net pay is " + netPay);
    System.exit(0);
    }
}
}

Thanks much,
Sam

Recommended Answers

All 4 Replies

(1)You have to assign the following variables an initial value, such as 0 for init or 0.0 for double in lines 10,11, and 12:
double withholding= 0.0;
double netPay= 0.0;
int withholding1= 0;
to pass the compiling.

(2) You have to delete the pair of curly brackets at the line 28 and the line 40

(1)You have to assign the following variables an initial value, such as 0 for init or 0.0 for double in lines 10,11, and 12:
double withholding= 0.0;
double netPay= 0.0;
int withholding1= 0;
to pass the compiling.

(2) You have to delete the pair of curly brackets at the line 28 and the line 40

Why does he have to delete those braces? Its perfectly fine to do that.

The bracket on line 28 I put because I seen it online that way, I'm not 100% on java structure for classes and methods, I'm learning though, thanks for the input.

Hi all, I got it, thanks much to your help. I did actually have to take the bracket on line 28 because it messed with my else if and if the gross was less than or equal to 300.00 it would show my the dialog message. Thanks much for the help and here is my final for anyone else to refer to.

import javax.swing.JOptionPane;
public class ComputeNet
{
    public static void main(String[] args)
    {
    String rateString;
    String hourString;
    double rate;
    double hours;
    double grossPay;
    double withholding = 0.00;
    double netPay = 0.00;
    int withholding1 = 0;
    rateString = JOptionPane.showInputDialog(null,
        "How much do you make per hour?");
    hourString = JOptionPane.showInputDialog(null,
        "How many hours did you work this week?");
    rate = Double.parseDouble(rateString);
    hours = Double.parseDouble(hourString);
    grossPay = rate * hours;
    if(grossPay <= 300)
    {
        withholding = .10;
        netPay = grossPay - (grossPay * withholding);
        withholding1 = 10;
    }
   else if(grossPay >= 300.01)
    {
        withholding = .12;
        netPay = grossPay - (grossPay * withholding);
        withholding1 = 12;
    }
    JOptionPane.showMessageDialog(null,
        "Your gross pay is $" + grossPay +
        ", Your withholding is " + withholding1 +
        " percent, and your net pay is $" + netPay);
    System.exit(0);
    }
}

Thanks again,
Sam

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.