Simple Salary Calculator Program
Hello, this is my first post on here, and I am new to java. I am trying to make a simple salary calculator that takes user input using the Scanner for the fields of entering an employee ID number, hourly pay, number of regular, and number of overtime hours worked. From there, it calculates the weekly pay with the employee ID number in the system.out somewhere. Here is what I have so far:
Code :
import java.util.Scanner;
public class PaymentCalculator {
int employeeId;
int regHours;
int overHours;
double wage;
double totalPay;
public static void main(String[] args) {
System.out.println("Enter Employee ID: ");
Scanner scan = new Scanner(System.in);
int employeeId = scan.nextint();
System.out.println("Enter the Number of Regular Hours: ");
Scanner scan1 = new Scanner(System.in);
int regHours = scan.nextint();
System.out.println("Enter the Number of Over Time Hours: ");
Scanner scan2 = new Scanner(System.in);
int overHours = scan.nextint();
System.out.println("Enter the Hourly Wage: ");
Scanner scan3 = new Scanner(System.in);
double wage = scan.nextdouble();
double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
System.out.println("The total pay this week is: " + totalPay);
}
}
I am using JCreator and am getting the errors:
--------------------Configuration: PaymentCalculator - JDK version 1.7.0_03 <Default> - <Default>--------------------
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\PaymentCalculator\PaymentCalculator .java:13: error: cannot find symbol
int employeeId = scan.nextint();
^
symbol: method nextint()
location: variable scan of type Scanner
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\PaymentCalculator\PaymentCalculator .java:16: error: cannot find symbol
int regHours = scan.nextint();
^
symbol: method nextint()
location: variable scan of type Scanner
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\PaymentCalculator\PaymentCalculator .java:19: error: cannot find symbol
int overHours = scan.nextint();
^
symbol: method nextint()
location: variable scan of type Scanner
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\PaymentCalculator\PaymentCalculator .java:22: error: cannot find symbol
double wage = scan.nextdouble();
^
symbol: method nextdouble()
location: variable scan of type Scanner
4 errors
Process completed.
Under the general output I am receiving:
--------------------Configuration: PaymentCalculator - JDK version 1.7.0_03 <Default> - <Default>--------------------
Error: Could not find or load main class PaymentCalculator
Process completed.
Any help with this would be much appreciated!
Re: Simple Salary Calculator Program
Java is case sensitive. . .'
Your code contains nextint() and nextdouble() . . .'
Replace them with nextInt() and nextDouble(). . .'
('I' and 'D' are capital)'
Re: Simple Salary Calculator Program
Also in line 14, 17 and 20, replace scan with scan1, scan2, scan3 respectively.'
Re: Simple Salary Calculator Program
Thanks, it is working now! Should I update the op with the corrections?
Re: Simple Salary Calculator Program
It would be kind of you to post the new code with corrections rather than change the op, so that people can understand what was changed later on. You can also mark this thread as solved up at the top of the page under thread tools.
Re: Simple Salary Calculator Program
Okay, will do.
Code :
import java.util.Scanner;
public class PaymentCalculator {
int employeeId;
int regHours;
int overHours;
double wage;
double totalPay;
public static void main(String[] args) {
System.out.println("Enter Employee ID: ");
Scanner scan = new Scanner(System.in);
int employeeId = scan.nextInt();
System.out.println("Enter the Number of Regular Hours: ");
Scanner scan1 = new Scanner(System.in);
int regHours = scan1.nextInt();
System.out.println("Enter the Number of Over Time Hours: ");
Scanner scan2 = new Scanner(System.in);
int overHours = scan2.nextInt();
System.out.println("Enter the Hourly Wage: ");
Scanner scan3 = new Scanner(System.in);
double wage = scan3.nextDouble();
double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
System.out.println("The Total Pay This Week Is $" + totalPay);
System.out.println("For Employee #" + employeeId);
}
}
Then I modified it so that it would take a day by day entry:
Code :
import java.util.Scanner;
public class Mod_of_3_4 {
//Constant Vars
int employeeId;
int regHours;
int overHours;
double wage;
double totalPay;
//Day 1 Var
int regHours1;
int overHours1;
//Day 2 Var
int regHours2;
int overHours2;
//Day 3 Var
int regHours3;
int overHours3;
//Day 4 Var
int regHours4;
int overHours4;
//Day 5 Var
int regHours5;
int overHours5;
public static void main(String[] args) {
//Constants
System.out.println("Enter Employee ID: ");
Scanner scan = new Scanner(System.in);
int employeeId = scan.nextInt();
System.out.println("Enter the Hourly Wage: ");
Scanner scan3 = new Scanner(System.in);
double wage = scan3.nextDouble();
//Day 1
System.out.println("Monday: Enter the Number of Regular Hours: ");
Scanner scan1 = new Scanner(System.in);
int regHours1 = scan1.nextInt();
System.out.println("Monday: Enter the Number of Over Time Hours: ");
Scanner scan2 = new Scanner(System.in);
int overHours1 = scan2.nextInt();
//Day 2
System.out.println("Tuesday: Enter the Number of Regular Hours: ");
Scanner scan4 = new Scanner(System.in);
int regHours2 = scan4.nextInt();
System.out.println("Tuesday: Enter the Number of Over Time Hours: ");
Scanner scan5 = new Scanner(System.in);
int overHours2 = scan5.nextInt();
//Day 3
System.out.println("Wednesday: Enter the Number of Regular Hours: ");
Scanner scan6 = new Scanner(System.in);
int regHours3 = scan6.nextInt();
System.out.println("Wednesday: Enter the Number of Over Time Hours: ");
Scanner scan7 = new Scanner(System.in);
int overHours3 = scan7.nextInt();
//Day 4
System.out.println("Thursday: Enter the Number of Regular Hours: ");
Scanner scan8 = new Scanner(System.in);
int regHours4 = scan8.nextInt();
System.out.println("Thursday: Enter the Number of Over Time Hours: ");
Scanner scan9 = new Scanner(System.in);
int overHours4 = scan9.nextInt();
//Day 5
System.out.println("Friday: Enter the Number of Regular Hours: ");
Scanner scan10 = new Scanner(System.in);
int regHours5 = scan10.nextInt();
System.out.println("Friday: Enter the Number of Over Time Hours: ");
Scanner scan11 = new Scanner(System.in);
int overHours5 = scan11.nextInt();
//Calculations
int regHours = (regHours1 + regHours2 + regHours3 + regHours4 + regHours5);
int overHours = (overHours1 + overHours2 + overHours3 + overHours4 + overHours5);
double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
//Print Results
System.out.println("The Total Pay This Week Is $" + totalPay);
System.out.println("For Employee #" + employeeId);
}
}
Thanks again for the help!
Re: Simple Salary Calculator Program
So. you look quite happy now....'
You can also try to reduce no. of lines of code.....which helps to run your program little faster....'
(I read it somewhere while Googling)'
Declare all variables in single line '
You can loop through blocks instead of writing such a large program..................'
Re: Simple Salary Calculator Program
Okay, I am trying to make the program run from a few methods and I'm running into errors left and right:
Code :
import java.util.Scanner; //Takes user input
import java.text.*; //import for the decimal format
public class Mod_of_3_4 {
//Constant Vars
int employeeId;
int regHours;
int overHours;
double wage;
double totalPay;
//Day 1 Var
int regHours1;
int overHours1;
//Day 2 Var
int regHours2;
int overHours2;
//Day 3 Var
int regHours3;
int overHours3;
//Day 4 Var
int regHours4;
int overHours4;
//Day 5 Var
int regHours5;
int overHours5;
public static void main(String[] args) {
//Methods
inputID();
inputWage();
inputDays();
calcPay();
calcTotal();
printResults();
}
//Employee ID Number
public int inputID() {
System.out.println("Enter Employee ID: ");
Scanner scan = new Scanner(System.in);
int employeeId = scan.nextInt();
}
//Hourly Wage
public double inputWage() {
System.out.println("Enter the Hourly Wage: ");
Scanner scan3 = new Scanner(System.in);
double wage = scan3.nextDouble();
}
//Input Daily Hours
public int inputDays() {
//Day 1
System.out.println("Monday: Enter the Number of Regular Hours: ");
Scanner scan1 = new Scanner(System.in);
int regHours1 = scan1.nextInt();
System.out.println("Monday: Enter the Number of Over Time Hours: ");
Scanner scan2 = new Scanner(System.in);
int overHours1 = scan2.nextInt();
//Day 2
System.out.println("Tuesday: Enter the Number of Regular Hours: ");
Scanner scan4 = new Scanner(System.in);
int regHours2 = scan4.nextInt();
System.out.println("Tuesday: Enter the Number of Over Time Hours: ");
Scanner scan5 = new Scanner(System.in);
int overHours2 = scan5.nextInt();
//Day 3
System.out.println("Wednesday: Enter the Number of Regular Hours: ");
Scanner scan6 = new Scanner(System.in);
int regHours3 = scan6.nextInt();
System.out.println("Wednesday: Enter the Number of Over Time Hours: ");
Scanner scan7 = new Scanner(System.in);
int overHours3 = scan7.nextInt();
//Day 4
System.out.println("Thursday: Enter the Number of Regular Hours: ");
Scanner scan8 = new Scanner(System.in);
int regHours4 = scan8.nextInt();
System.out.println("Thursday: Enter the Number of Over Time Hours: ");
Scanner scan9 = new Scanner(System.in);
int overHours4 = scan9.nextInt();
//Day 5
System.out.println("Friday: Enter the Number of Regular Hours: ");
Scanner scan10 = new Scanner(System.in);
int regHours5 = scan10.nextInt();
System.out.println("Friday: Enter the Number of Over Time Hours: ");
Scanner scan11 = new Scanner(System.in);
int overHours5 = scan11.nextInt();
}
//Reformat and Calculate
public int calcPay() {
//Decimal Format
DecimalFormat df = new DecimalFormat("#.##");
//Calculations
int regHours = (regHours1 + regHours2 + regHours3 + regHours4 + regHours5);
int overHours = (overHours1 + overHours2 + overHours3 + overHours4 + overHours5);
}
public double calcTotal() {
double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
}
//Print Results
public void printResults() {
System.out.println("Total pay for the week is: $"+df.format(totalPay));
System.out.println("For Employee #" + employeeId);
}
}
I'm getting the error messages:
Quote:
--------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: non-static method inputID() cannot be referenced from a static context
inputID();
^
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: non-static method inputWage() cannot be referenced from a static context
inputWage();
^
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: non-static method inputDays() cannot be referenced from a static context
inputDays();
^
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: non-static method calcPay() cannot be referenced from a static context
calcPay();
^
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: non-static method calcTotal() cannot be referenced from a static context
calcTotal();
^
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: non-static method printResults() cannot be referenced from a static context
printResults();
^
C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:130: error: cannot find symbol
System.out.println("Total pay for the week is: $"+df.format(totalPay));
^
symbol: variable df
location: class Mod_of_3_4
7 errors
Process completed.
I've tried to fix the errors and I'm only getting more, any help would be much appreciated! Also, how can I do the loops mentioned in the post above?