I've wrote code to loop though details entered about an employees shift but when it loops through the second time it doesn't wait for a user input when asking for the employees name? It skips onto the next user input for how many shifts they work and also takes a blank name for the second run through so its not holding onto the previous entrys name and using that.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class GetHoursWorked {
@SuppressWarnings("static-access")
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int noShiftsWorked;
String employeeName = null;
int employeeNumber=1;
int i=0;
int j=0;
int k=0;
int split =0;
long totalBreaks=0;
long totalHours=0;
Date date = null,startTime = null,finishTime = null;
while(employeeNumber !=0) {
System.out.printf("Please enter employees name: ");
employeeName = keyboard.nextLine();
ArrayList<Shift> shifts = new ArrayList<Shift>(); //creating an arraylist of shifts for the employee
System.out.printf("How many shifts has %s worked this week?", employeeName);
while (!keyboard.hasNextInt()) { //checking if user entered a valid number for the amount of shifts worked
String input = keyboard.next();
System.out.printf("\"%s\" is not a valid number. Please enter a valid number %n", input);
}
noShiftsWorked =keyboard.nextInt();//assigning number of shifts
for(i=0;i<noShiftsWorked;i++) { //loop through each shift and entering details
//ensuring user enters a valid date in the format described
for(j=0;j<1;j++) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Scanner sc = new Scanner(System.in);
System.out.printf("Enter date of shift %s (example:10/03/2022): ",i+1);
String str = sc.nextLine();
try {
date = sdf.parse(str);
sdf = new SimpleDateFormat("EEE, d MMM yyyy");
System.out.println("Date: " + sdf.format(date));
}
catch (ParseException e) {
System.out.println("Please enter a valid date:");
j=-1;
}
}
//Loop to ensure user enters valid start time
for(j=0;j<1;j++) {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Scanner sc = new Scanner(System.in);
System.out.print("Enter Start time (example:10:15) ");
String start = sc.nextLine();
try {
startTime = sdf.parse(start);
sdf = new SimpleDateFormat("hh:mm");
} catch (ParseException e) {
System.out.println("Please enter a valid time:");
j=-1;
}
}
//Loop to ensure user enters valid finish time
for(j=0;j<1;j++) {
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Scanner sc = new Scanner(System.in);
System.out.print("Enter Finish time (example:21:15) ");
String finish = sc.nextLine();
try {
finishTime = sdf.parse(finish);
sdf = new SimpleDateFormat("hh:mm");
} catch (ParseException e) {
System.out.println("Please enter a valid time:");
j=-1;
}
}
//converting times into number of hours worked
long hoursWorked = ((finishTime.getTime() - startTime.getTime())/(1000 * 60 * 60))% 24;
if (hoursWorked <0) {
hoursWorked = hoursWorked+24;
}
Shift newShift = new Shift(date,startTime,finishTime);
newShift.setHoursWorked(hoursWorked);
//looping through the shifts already in entered to check
//for matching dates and assigning the shifts as a split shift if they match
for (Shift d: shifts) {
if (d.date == newShift.date) {
d.setIsSplitShift(true);
long firstShift = d.getHoursWorked();
long secondShift = newShift.getHoursWorked();
long totalShift = firstShift+secondShift;
d.setHoursWorked(totalShift);
split = 1;
}
}
// adding the shift to the Arraylist of shifts if it is not a split
if(split!=1) {
newShift.setIsSplitShift(false);
shifts.add(newShift);
}
}
//looping through all shifts in the
//Arraylist and getting total hours worked and total breaks allowed
for(Shift e:shifts) {
e.setBreakTime(e.getHoursWorked(),e.getIsSplitShift(),e.getIsNightShift(),totalBreaks);
totalHours=totalHours+(e.getHoursWorked());
totalBreaks=totalBreaks+(e.getBreakTime());
}
System.out.printf("%s worked a total of %d hours and is entitled to a total of %d minutes break %n",employeeName,totalHours,totalBreaks);
System.out.println("Do you want to enter details for another employee?");
//asking user if they want to enter another employees details
//and ending program if not while looping back through the form if they do
for (k=0;k<1;k++) {
String anotherEntry = keyboard.next().toUpperCase();
//making sure whatever case of yes user enters it still validates response
if (anotherEntry.equals("YES")) {
k=1;
}
else if(anotherEntry=="NO") {
k=1;
employeeNumber=0;
System.out.println("OK thank you,have a nice day");
}
else {
System.out.println("You did not enter a valid response,please respond either yes or no:");
k=-1;
}
}
}
}
}