I'm trying to get the person's name from an input prompt. Get the name length and if the length is less than 1 throw a custom error throws LineLimitException, and prompt the user again to enter their name. I know I threw a lot of code in here but I'm hoping it makes what I'm trying to do more clearly.
LineLimitException Class
public LineLimitException(String message){
super(message);
}
}
Here I prompt and store the name field
name = input.nextLine();
int strSizeName = name.length();
if(strSizeName < 1) {
I'm getting these results in the console
How many employees are there?
2
Enter employee 1's name
Name must be greater than one character. Please try again.
Enter employee 1's name *<--- this is before I can even enter a name*
Enter employee's hourly wage *<-- prompts for hourly wage before I got the name.*
public class Driver {
/**
* defualt constructor
*/
public Driver() {
}
/**
* Main method - prompts the user for name, hours worked, and hourly salary. Stores it in a person object
* @author
* @param args
*
*/
public static void main(String[] args) throws LineLimitException{
ArrayList<Employee> person = new ArrayList<Employee>();
// prompt user for the total number of employees
Scanner input = new Scanner(System.in);
System.out.println("How many employees are there?");
String name;
int numEmployees = input.nextInt();
for (int count = 1; count <= numEmployees; count++) {
System.out.println("Enter employee " + count + "'s name");
**name = input.nextLine();
int strSizeName = name.length();
if(strSizeName < 1) {
//Why is this showing up afterI give emploee count?
System.out.println("Name must be greater than one character. Please try again.");
//reprompt the user for a name
System.out.println("Enter employee " + count + "'s name");
}else {
continue;
}**
System.out.println("Enter employee's hourly wage");
double wage = input.nextDouble();
System.out.println("Enter how many hours worked");
double hrsWkd = input.nextDouble();
person.add(new Employee(name, wage, hrsWkd)); // add an employee object to the ArrayList
printSalaryReport(person, wage, hrsWkd);
}
input.close();
System.out.println("CSV File Created.");
}
/**
* Method printSalaryReport - prints the employee person objects name, hours worked
* hourly salary and gross salary to a csv file.
* @param person in ArrayList
* @param getHours_Worked
* @param getHourly_Salary
* @throws IncorrectFileNameException
*/
private static void printSalaryReport(ArrayList<Employee> person, double getHours_Worked, double getHourly_Salary) {
String CSV_SEPARATOR = ",";
String fileName = "employee.csv";
try
{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));
String Header = "Name, Hourly Salary, Hours Worked, Pay";
bw.write(Header.toString());
bw.newLine();
for (Employee man : person)
{
StringBuffer manStr = new StringBuffer();
manStr.append(man.getName());
manStr.append(CSV_SEPARATOR);
manStr.append(man.getHourly_Salary());
manStr.append(CSV_SEPARATOR);
manStr.append(man.getHours_Worked());
manStr.append(CSV_SEPARATOR);
manStr.append(calculateSalary(man.getHours_Worked(), man.getHourly_Salary()));
bw.write(manStr.toString());
bw.newLine();
}
bw.flush();
bw.close();
}
catch (IOException err){}
}
/**
* Method calculateSalary takes the hours worked and hourly salary and returns
* the result of each multiplied.
*
* @param getHours_Worked
* @param getHourly_Salary
* @return
*/
private static Object calculateSalary(double getHours_Worked, double getHourly_Salary) {
double salary = getHourly_Salary;
double hoursWorked = getHours_Worked;
double wkAmount = salary*hoursWorked;
return wkAmount;
}
}