0

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;
    }

}
JenPann
  • 29
  • 7
  • 1
    When you throw uncatched exception, you program ends so you do not need throw that if you want user to specify name anyway. Prompt for input in loop, print diagnostic if error and ask again. – S. Kadakov Mar 09 '21 at 14:26
  • I'm sorry I don't understand what I need to do – JenPann Mar 09 '21 at 14:31
  • you wrote: "if the length is less than 1 throw a custom error throws LineLimitException, and prompt the user again to enter their name". So, you need ask user to input the name in a loop without throwing any exception because when you throw that, main method will be terminated and program will end. – S. Kadakov Mar 09 '21 at 14:35
  • That's not good. my Assignment wants us to create a custom exception and use it. Having the whole program stop isn't what I was hoping for – JenPann Mar 09 '21 at 14:48
  • But you wrote exactly that: public static void main(String[] args) throws LineLimitException. So, if you throw this exception anywhere within your code, program will exit with error massage and stack dump. If you want to manage that, you must catch exceprion when it will be thrown. – S. Kadakov Mar 09 '21 at 15:04

1 Answers1

1
    boolean skip = false;
    for (int count = 1; count <= numEmployees; count++) {
        if (!skip)
            input.nextLine();
        else
            skip = false;

        try {
            System.out.println("Enter employee " + count + "'s name");

            name = input.nextLine();
            int strSizeName = name.length();
            if (strSizeName < 1)
                throw new LineLimitException("Name must be non-empty. Please try again.");

            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);
        } catch (final LineLimitException e) {
            System.out.println(e.getMessage());
            count--;
            skip = true;
        }
    }

Does this solve your problem?

Brongs Gaming
  • 40
  • 1
  • 8