Hey guys,

This is my first topic.
I tried my very best to keep behind the scenes and not make a thread about this, but its been ticking me off for a while now.. The "while" loop im using.. i can only get the last user input to display or to be used in math equations.. when i need every user input to be used. the program I am working on consists of getting employee salary and pay rate information.. SO heres the code:

import javax.swing.JOptionPane;

public class Main {

public static void main(String[] args) {

//
String strOption, strHour, strSalary, strPay, strWorker;
int option;
int worker;
int selections;
double hour;
double pay = 0;
double salary = 0;
double month;
double week = 0;

selections = 0;

strOption = JOptionPane.showInputDialog("Menu options:\n1. Enter Hourly Employee\n2. Enter Salaried Employee\n0. Done\n Please select a menu option:");

option = Integer.parseInt(strOption);

//start outer


while (option >= 3) {
JOptionPane.showMessageDialog(null, "Sorry, " + (option) + " is not a valid option.");
strOption = JOptionPane.showInputDialog("Menu options:\n1. Enter Hourly Employee\n2. Enter Salaried Employee\n0. Done\n Please select a menu option: ");
option = Integer.parseInt(strOption);
}


switch (option) {
case 0:

JOptionPane.showMessageDialog(null, "Goodbye.");

break;

case 1:
strWorker = JOptionPane.showInputDialog("how many employees are on hourly base pay?");
worker = Integer.parseInt(strWorker);
strHour = JOptionPane.showInputDialog("Enter the number of hours per week:");
hour = Double.parseDouble(strHour);
while (selections < worker) {
strPay = JOptionPane.showInputDialog("Enter the hourly pay rate :");
pay = Double.parseDouble(strPay);

selections++;


}


JOptionPane.showMessageDialog(null, (pay));
break;





case 2:
selections = 1;
strWorker = JOptionPane.showInputDialog("how many employees?");
worker = Integer.parseInt(strWorker);

while (selections <= worker) {
strSalary = JOptionPane.showInputDialog("Enter the Yearly salary:");
salary = Double.parseDouble(strSalary);
selections++;

}
selections = worker;
month = salary / 4.3;

JOptionPane.showMessageDialog(null, (month));





}


strOption = JOptionPane.showInputDialog("Menu options:\n1. Enter Hourly Employee\n2. Enter Salaried Employee\n0. Done\n Please select a menu option: ");
System.exit(0);
}

}

another problem im having is that i cant seem to get the menu option to revert back to the case statements after execution. case .. it just exits.. =( and yet another problem is that when i click cancel.. it exits with an error about exception in Main thread.. if i can get these little things cleared up i can actually the rest of the things i need to do like make the math equations.. but i cant do that until these problems are solved.. thanks guys

Here's something to get you started. Create a class called HourlyEmployee for the hourly employees:

HourlyEmployee:

public class HourlyEmployee {
    protected double payRate;
    protected double hoursPerWeek;

    public HourlyEmployee(){

    } //constructor

     public void setPayRate(double newPayRate)
     {
          payRate = newPayRate;
     }

     public double getPayRate()
     {
          return payRate;
     }

     public void setHoursPerWeek(double newHoursPerWeek)
     {
          hoursPerWeek = newHoursPerWeek;
     }

     public double getHoursPerWeek()
     {
          return hoursPerWeek;
     }
}

If you are going to use the user input dialog, you should do something like the following:

isInteger:

private static boolean isInteger(String s) {
        if (s.length() >= 1){
            return java.util.regex.Pattern.matches("\\d+", s);
        }
        else
        {
            //return false if string is null
            return false;
        }
    }//isInteger
do {
     strOption = JOptionPane.showInputDialog("Menu options:\n1. Enter Hourly Employee\n2. Enter Salaried Employee\n0. Done\n Please select a menu option: ");
     if (strOption == null){
          //happens when cancel is pressed
         JOptionPane.showMessageDialog(null, "You pressed cancel. Exiting...");

          //return; //"exits" the method
     }//if
     else if (option > 2 || isInteger(strOption) != true){
          JOptionPane.showMessageDialog(null, "Invalid option: " + strOption);
     }//if
     else{
          option = Integer.parseInt(strOption);
          //we have a valid option so continue with the program


     }//else
} while (option >2 || isInteger(strOption) != true);

Then create an ArrayList of hourly employees.

import java.util.ArrayList;

//create an ArrayList of type HourlyEmployee
ArrayList <HourlyEmployee> myHourlyEmployees = new HourlyEmployee();
HourlyEmployee hEmployee = new HourlyEmployee();

//prompt for pay rate; I'll let you fill in that code
//we'll call it  "strPay"

//convert to double
pay = Double.parseDouble(strPay);
hEmployee.setPayRate(pay);

//prompt for number of hours worked; I'll let you fill in that code
//we'll call it: strHour

//convert to double
hour = Double.parseDouble(strHour);
hEmployee.setHoursPerWeek(hour);

myHourlyEmployees.add(hEmployee);

It's not quite clear what the goal of your program is.

Here is an example to calculate the total amount to be paid to all hourly employees:

ArrayList <HourlyEmployee> myHourlyEmployees = new HourlyEmployee();

//To Do: get menu option: 

int menuOption = Integer.parseInt(strOption);

switch (menuOption){
     case 0: 
          //exit method
          return;
     case 1:
          //To Do: get number of employees: strWorker

          int numberOfHourlyEmployees = Integer.parseInt(strWorker);
    
          int count = 0;
          while (count < numberOfHourlyEmployees){
               //create new hEmployee each loop
               HourlyEmployee hEmployee = new HourlyEmployee();

               //To Do: get hourly pay rate: strPay

               double pay = Double.parseDouble(strPay);
               hEmployee.setPayRate(pay);

               //To Do: get number hours per week for employee: strHour

               double hour = Double.parseDouble(strHour);
               hEmployee.setHoursPerWeek(hour);

               //optionally, add "salary" to class HourlyEmployee
               //then: salary = hEmployee.getHoursPerWeek() * hEmployee.getPayRate();
               //and: hEmployee.setSalary(salary);

               //or you could add a method, called "salary" to class HourlyEmployee          

               //add hEmployee to myHourlyEmployees ArrayList
               myHourlyEmployees.add(hEmployee);
          }//while


          double totalHourlyEmployeePay=0.0;
          for (int i = 0; i < myHourlyEmployees.size(); i++){
               //create new hEmployee each loop
               HourlyEmployee hEmployee = new HourlyEmployee();
               hEmployee = myHourlyEmployees.get(i);
               double monthlyEmployeeSalary = hEmployee.getPayRate() * hEmployee.getHoursPerWeek();
               totalHourlyEmployeePay = totalHourlyEmployeePay + monthlyEmployeeSalary;
          }//for     

          JOptionPane.showMessageDialog(null, (totalHourlyEmployeePay));
          break;
     case 2:

          break;
}//switch

im kind of confused.. I understood everything up to the "isInteger" code.. so am i suppsoed to make a new class called "isInteger" and put that code there.. or am I adding that portion to my "main" class?

In the example I posted, you would put it in the main class. It is just for data validation. If you are able to assume that the user enters the "proper" information, you don't need it. In my opinion, though, one should never assume that the user will enter the proper information.

public class Main {
    private static boolean isInteger(String s) {
        if (s.length() >= 1){
            return java.util.regex.Pattern.matches("\\d+", s);
        }
        else
        {
            //return false if string is null
            return false;
        }
    }//isInteger

public static void main(String[] args) {
     //your code goes here
}

I made a mistake in one of my previous posts. A simple fix is to split the following line into two else if statements:

else if (option > 2 || isInteger(strOption) != true)

We need to check to see if strOption is only digits before we can parse it to an integer. And we have to parse it to an integer before we can check to see if it is > 2. See the code below:

do {
     strOption = JOptionPane.showInputDialog("Menu options:\n1. Enter Hourly Employee\n2. Enter Salaried Employee\n0. Done\n Please select a menu option: ");

     if (strOption == null){
          //happens when cancel is pressed
          JOptionPane.showMessageDialog(null, "You pressed cancel. Exiting...");

          return; //"exits" the method
     }//if
     else if (isInteger(strOption) != true){
          JOptionPane.showMessageDialog(null, "Sorry, " + (strOption) + " is not a valid option.");
     }//if
     else if (Integer.parseInt(strOption) > 2){
          //value has been validated as an integer above
          //so check to see if value is > 2
          JOptionPane.showMessageDialog(null, "Sorry, " + (strOption) + " is not a valid option.");
     }//else if
     else{
          option = Integer.parseInt(strOption);          
          //we have a valid option so continue with the program


     }//else
} while (option >2 || isInteger(strOption) != true);

Here's an alternative "isInteger" method. In case you are not familiar with regular expressions.

isInteger

private static boolean isInteger(String s){
        char[] myCharArray;
        boolean returnValue = true;

        //convert string to Character array
        myCharArray = s.toCharArray();

        //check each character to see if it is a digit
        //if any character is NOT a digit, set returnValue = false
        for (int i=0; i < myCharArray.length; i++){
  
            //if we find a character that is NOT a digit, 
            //return false
            if (!Character.isDigit(myCharArray[i]) ){
                returnValue = false;
            }
        }//for

        return(returnValue);
    }//isInteger

Here's a regular expression to check if a number is a double:

if (s.length() >= 1){
  return java.util.regex.Pattern.matches("[\\d+]|[\\d]+.[\\d]+", s);
}//if

In the above regular expression, I check to see if the string is made up of all digits OR a series of digits followed by a "." followed by more digits. How could you write it without regular expressions?

thanks so much for the hlp.. i turned the program in.. but.. im just a little curious with setting up the array would you set up the
public static void main(String[] args) { ? is it public abstract when using arrays? when i tried setting it up i could an error message saying it was invalid data types

Post what you tried, and so we can see why it didn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.