Alright, so I have a while loop that is made to execute while the length of a string is 0, and when the string is initialized as an empty string it works just fine for the first pass, but after completing the rest of the loop that this loop is nested in, it just breaks and works in a completely unintended manner. Could somebody examine this for me and help me figure out whats wrong?
import java.util.*;
public class Project5
{
public static void main(String[] args)
{
List namesLinkedList = new LinkedList();//creates a new linked list called namesLinkedList
List ageLinkedList = new LinkedList();//creates a new linked list called ageLinkedList
List salaryLinkedList = new LinkedList();//creates a new linked list called salaryLinkedList
int length = loadLists(namesLinkedList, ageLinkedList, salaryLinkedList);//
printLists(namesLinkedList, ageLinkedList, salaryLinkedList,length);
}
public static int loadLists(List namesLinkedList, List ageLinkedList, List salaryLinkedList)
{
Scanner input = new Scanner(System.in);
String check1 = new String();
int check2 = 0;
int check3 = 0;
String name = new String();
int age = 0;
int salary = 0;
int count = 0;
while(check1 != "9999")
{
while(name.equals(""))
{
System.out.print("Please enter an employee name: ");
name = input.nextLine().trim();
if(name.length() == 0)
{
System.out.println("An employees name can't be an empty string.");
}
else
{
check1 = name;
}
}
if(check1.equals("9999"))
{
break;
}
while(check2 > 68 || check2 < 23)
{
System.out.print("Please enter an age for this employee: ");
age = input.nextInt();
if(age > 68 || age < 23)
{
System.out.println("This age is not within the allowed range.( 23-68)");
}
else
{
check2 = age;
}
}
while(check3 > 120000 || check3 < 30000)
{
System.out.print("Please enter a salary for this employee: ");
salary = input.nextInt();
if(salary > 120000 || salary < 30000)
{
System.out.println("This salary is not within the allowed range(30000-120000).");
}
else
{
check3 = salary;
}
}
namesLinkedList.add(name);
ageLinkedList.add(age);
salaryLinkedList.add(salary);
check1 = "";
name = "";
age = 0;
salary = 0;
count++;
}
return count;
}
public static void printLists(List namesLinkedList, List ageLinkedList, List salaryLinkedList, int listLength)
{
System.out.printf("%-20s %-20s %14s\n", "Name","Age", "Salary");//prints a header
for(int i = 0; i < listLength; i++)
{
System.out.printf("%-20s %-20s %14s\n", namesLinkedList.get(i),ageLinkedList.get(i),salaryLinkedList.get(i));
}
}
}
and the output ends up being something like this:
Please enter an employee name: Ace
Please enter an age for this employee: 12
This age is not within the allowed range.( 23-68)
Please enter an age for this employee: 34
Please enter a salary for this employee: 12000
This salary is not within the allowed range(30000-120000).
Please enter a salary for this employee: 45000
Please enter an employee name: An employees name can't be an empty string.
Please enter an employee name: Ellie
Please enter an employee name: Elise
Please enter an employee name: Alan
Please enter an employee name: 9999
Name Age Salary
Ace 34 45000
Ellier 0 0
Elise 0 0
Alan 0 0
As shown here, the loop fails to execute properly any time after the first. It's as if the name.length() value isnt properly resetting