Re: Symbol not found error
There is no variable named type, perhaps you meant to use typeH instead.
Also, it is almost never a good idea to compare String with ==. String are objects, so you should use the .equals() method instead.
Re: Symbol not found error
now I'm gettinga null pointer at line 53 (typeH = "salaried";)
Code :
import java.util.*;
public class Employee2 {
//Varibales
private static double salary, rate, hours, weeklyPay = 0;
private static String name;
private static String typeH;
static boolean bonusPay = false;
//Constructor creates Employee object
public Employee2(String name, String status, double hours, double rate, double salary, boolean bonusPay) {
if (status.equals("salaried")) {
this.salary = salary;
if(bonusPay == true) {
salary = salary * 1.1;
}
} else {
this.rate = rate;
this.hours = hours;
if(hours > 40) { //calculates and adds overtime pay
weeklyPay = (hours - 40) * (2*(salary)) + 40 * salary;
} else {
weeklyPay = rate * hours;
}
}
}
//Methods to obtain information about a specific employee
private double getWeeklyPay() {
return weeklyPay;
}private String getName() {
return name;
}private double getSalary() {
return salary;
}private String getType() {
return type;
}private double getHours() {
return hours;
}private double getRate() {
return rate;
}
private void printData(Employee2 x) {
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hello! Welcome to Employee Database");
System.out.println("If Employee is salaried enter S. Otherwise enter H for hourly");
if(input.nextLine().toLowerCase().contains("s")) {
typeH = "salaried";
} else if(input.nextLine().toLowerCase().contains("h")) {
typeH = "hourly";
}
System.out.println("Enter Employee's full name");
name = input.nextLine();
if(typeH.equals("salaried")) {
System.out.println("Enter weekly salary");
salary = input.nextDouble();
System.out.println("Enter Y if employee receives a bonus of 10%, otherwise enter N");
if(input.nextLine().toLowerCase().contains("y")) {
bonusPay = true;
} else {
bonusPay = false;
}
} else if ( typeH.equals("hourly")) {
System.out.println("Enter hourly pay");
rate = input.nextDouble();
System.out.println("Enter # of hours");
hours = input.nextDouble();
}
Employee2 Employee1 = new Employee2(name, type, rate, hours, salary, bonusPay);
System.out.println();
System.out.println("\t Name Status Hours Rate Weekly Pay Amount");
System.out.println("\t======================================================================");
System.out.println(Employee1.getName() + Employee1.getType() + Employee1.getHours() + Employee1.getRate() + Employee1.getWeeklyPay());
System.out.println();
}
}
Re: Symbol not found error
Most likely because typeH has not been initalized. Look at this code:
Code java:
if(input.nextLine().toLowerCase().contains("s")) {
typeH = "salaried";
} else if(input.nextLine().toLowerCase().contains("h")) {
typeH = "hourly";
}
The Scanner.nextLine() method moves the pointer. So every time you call the nextLine() method, you are getting a different line. Is that what you intend on doing? What you should do is create a local String variable called line, or something, set line = input.nextLine(), and then use that in your if/else statements instead.
Re: Symbol not found error
Thank you for your response, it is appreciated.
I have a further problem.
Code :
import java.util.*;
public class Employee2 {
//Varibales
private static double salary, rate, hours, weeklyPay = 0;
private static String name;
private static String typeH;
static boolean bonusPay = false;
//Constructor creates Employee object
public Employee2(String name, String status, double hours, double rate, double salary, boolean bonusPay) {
if (status.equals("salaried")) {
this.salary = salary;
if(bonusPay == true) {
salary = salary * 1.1;
}
} else {
this.rate = rate;
this.hours = hours;
if(hours > 40) { //calculates and adds overtime pay
weeklyPay = (hours - 40) * (2*(salary)) + 40 * salary;
} else {
weeklyPay = rate * hours;
}
}
}
//Methods to obtain information about a specific employee
private double getWeeklyPay() {
return weeklyPay;
}private String getName() {
return name;
}private double getSalary() {
return salary;
}private String getType() {
return type;
}private double getHours() {
return hours;
}private double getRate() {
return rate;
}
private void printData(Employee2 x) {
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
private string line;
System.out.println("Hello! Welcome to Employee Database");
System.out.println("If Employee is salaried enter S. Otherwise enter H for hourly");
line = input.nextLine();
if(line.toLowerCase().contains("h")) {
line = "salaried";
} else if()) {
line = "hourly";
}
System.out.println("Enter Employee's full name");
name = input.nextLine();
if(line.equals("salaried")) {
System.out.println("Enter weekly salary");
salary = input.nextDouble();
System.out.println("Enter Y if employee receives a bonus of 10%, otherwise enter N");
if(input.nextLine().toLowerCase().contains("y")) {
bonusPay = true;
} else {
bonusPay = false;
}
} else if ( line.equals("hourly")) {
System.out.println("Enter hourly pay");
rate = input.nextDouble();
System.out.println("Enter # of hours");
hours = input.nextDouble();
}
Employee2 Employee1 = new Employee2(name, type, rate, hours, salary, bonusPay);
System.out.println();
System.out.println("\t Name Status Hours Rate Weekly Pay Amount");
System.out.println("\t======================================================================");
System.out.println(Employee1.getName() + Employee1.getType() + Employee1.getHours() + Employee1.getRate() + Employee1.getWeeklyPay());
System.out.println();
}
}
When i execute it.
--------------------Configuration: <Default>--------------------
Hello! Welcome to Employee Database
If Employee is salaried enter S. Otherwise enter H for hourly
h
it stays at this indefinitely, so there is something wrong with the if loops
Re: Symbol not found error
That shouldn't even compile.
Not only is the if statement empty, but there is an extra bracket.
Furthermore:
string should be a capital S: String