Error says variable already defined in main when trying to convert string to double then says unknown symbol

StackOverflow https://stackoverflow.com/questions/14575471

문제

I have looked for over 40 minutes trying to find an answer on this site that might pertain to my question to no avail.

I am stumped. I am trying to convert a program to GUI. I am using Textpad which is telling me when I compile that the variable is already defined in main method. Then when converting to int or double, it is telling me that it cannot find symbol and points to the variable that I am trying to convert. Then during my calculations, it is telling me bad operands type for binary operator '*'.

import javax.swing.JOptionPane;

public class PayrollGUI {

// calculates payroll
public static void main (String [] args) {

String name = JOptionPane.showInputDialog ("Employee's Name: ");
int name = Integer.parseInt(nameString);

String hours = JOptionPane.showInputDialog ("Number of hours worked in a week (e.g., 10: ");
int hours = Integer.parseInt(hoursString);

String payRate = JOptionPane.showInputDialog ("Hourly pay rate (e.g., .6.75: ");
double payRate = Double.parseDouble(payRateString);

String federalTaxRate = JOptionPane.showInputDialog ("Federal tax witholding rate (e.g., .20: ");
double federalTaxRate = Double.parseDouble(federalTaxRateString);

String stateTaxRate = JOptionPane.showInputDialog (" State tax witholding rate (e.g., .09: ");
double stateTaxRate = Double.parseDouble(stateTaxRateString);

//calculate witholdings, grosspay and netpay
double federalWitholding = federalTaxRate * (hours * payRate);
double stateWitholding = stateTaxRate * (hours * payRate);
double grossPay = hours * payRate;
double netPay = grossPay - withholdings;
double witholdings = federalWithodling + stateWitholding;

//format to keep two digit decimal
witholdings = (int) (witholdings * 100) /100.0;
netPay = (int) (netPay * 100) / 100.0;
grossPay = (int) (grossPay * 100) / 100.0;
federalWitholding = (int) (federalWitholding * 100) / 100.0;
stateWitholding = (int) (stateWitholding *100) / 100.0;

/*String output = (null);
String output = (null);*/
String output = "Employee Name: " + name +
"/nHours Worked: "  + hours +
"/nPay Rate: $" + payRate +
"/nGross Pay: $" + grossPay +
"/nDeductions:" +
"/n     Federal Witholding: $" + federalWitholding +
"/n     State Witholding : $" + stateWitholding +
"/n     Total Deductions : $" + witholdings +
"/n     Net Pay: $";
JOptionPane.showMessageDialog(null, output);
}//end main
}//end Payroll
도움이 되었습니까?

해결책

String name = ...;
int name = ...;

Can't do it.

Replace String name with String nameString since that is the variable you are trying to parse when you use Integer.parseInt! (And don't forget all your other variables too!)

e.g.

String hours with String hoursString
String payRate with String payRateString
String federalTaxRate with String federalTaxRateString
String stateTaxRate with String stateTaxRateString

다른 팁

  1. You cannot compile since the name, hours, payRate, .... variables are declared twice. Change each of them to another name
  2. Your parseInt, parseDouble statements are not good, since there are possibly chances that the input is not in the correct format (alphabet or something). Therefore, you need to use try/catch to validate your input
  3. witholdings = (int) (witholdings * 100) /100.0; This is not the way to keep 2 digit format. Read this
  4. Try to use a better IDE. If you are new to java, have a look at jcreator or bluej

You cannot have two same variable names in one method. In your code you have many duplicate variables just like the one below. You cannot have two name variables in same method.

String name = JOptionPane.showInputDialog ("Employee's Name: ");
int name = Integer.parseInt(nameString);

If you use any IDE for Java like Eclipse, you can easily solve these kind of compilation issues rather than Textpad.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top