user9081540
user9081540

Reputation:

Not asking for input at end of while loop

I'm completely new to Java coding and I would really appreciate help on an assignment. I'm creating a program that will collect employee information such as name, pay, hours worked, and dependents.

Here is what I have so far:

package payroll;
import java.util.*;
import java.io.*;
/**
 *
 * @author whitn
 */
public class Payroll {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    String[]namearray;                       //Setting up the arrays
    namearray = new String[1000];            //Assume there are not 1000+ people
    double[]hrs_worked;
    hrs_worked = new double[1000];
    double[]payarray;
    payarray = new double[1000];
    int[]dependarray;
    dependarray = new int[1000];

    String name = "";                            //Initialize input
    float hrs;
    double pay;
    int dependents;
    int Q = -1;

    System.out.println("Please input the employee name:"); //Collect employee info
    name = kb.nextLine();

    while( name != null)
    {
        System.out.println("Please input the hours the employee worked:");
        hrs = kb.nextFloat();
        System.out.println("Please input the employee's hourly pay:");
        pay = kb.nextDouble();
        System.out.println("Please input the number of dependents the employee"
                + " claims:");
        dependents = kb.nextInt();
        Q++;

        namearray[Q] = name;
        payarray[Q] = pay;   
        hrs_worked[Q] = hrs;
        dependarray[Q] = dependents;


        System.out.println("\n Please input the employee name or press enter to"
                + " end the process:");
        name = kb.nextLine();
    }

The problem I run into is at the end of the while loop it will ask for the next person's name and instead of waiting for an input, the while loop starts over and immediately asks for the hrs, pay, dependents. There is no room to input a String name.

I would really appreciate any insight one can give to help me proceed. Like I said before, I'm new to coding and if I've left information out, please let me know!

Thank you

Upvotes: 0

Views: 79

Answers (1)

user1531038
user1531038

Reputation: 266

I am not sure what you are trying to achieve but you can try out below code with some modifications.

public static void main(String[] args) 
{
    Scanner kb = new Scanner(System.in);
    String[]namearray;                       //Setting up the arrays
    namearray = new String[1000];            //Assume there are not 1000+ people
    double[]hrs_worked;
    hrs_worked = new double[1000];
    double[]payarray;
    payarray = new double[1000];
    int[]dependarray;
    dependarray = new int[1000];

    String name = "";                            //Initialize input
    float hrs;
    double pay;
    int dependents;
    int Q = -1;

    while( true)
    {
        if(Q>-1)
        {
            System.out.println("\nPlease input the employee name or press enter to end the process:");
            name = kb.nextLine();

        }
        else
            System.out.println("Please input the employee name:"); //Collect employee info

        name = kb.nextLine();
        if(name==null || name.trim().equals("")) break;

        System.out.println("Please input the hours the employee worked:");
        hrs = kb.nextFloat();
        System.out.println("Please input the employee's hourly pay:");
        pay = kb.nextDouble();
        System.out.println("Please input the number of dependents the employee"
                + " claims:");
        dependents = kb.nextInt();
        Q++;

        namearray[Q] = name;
        payarray[Q] = pay;   
        hrs_worked[Q] = hrs;
        dependarray[Q] = dependents;
    }
}

Upvotes: 1

Related Questions