0

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

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
Iccirrus
  • 11
  • 1
  • 2
  • 4
  • 1
    Learn to use debuggers. – Sotirios Delimanolis Mar 09 '14 at 04:52
  • Please Reduce the Code only to the portion Where you think you are facing Problem – Adnan Ahmad Khan Mar 09 '14 at 04:55
  • Sotirios, if you have nothing useful to say, then save your time and be productive somewhere else. Adnan,I posted the whole thing because im not exactly sure where the problem may be. – Iccirrus Mar 09 '14 at 04:56
  • @user2844837 It looks like you took Sotirios' comment as rude, but regardless of that, it is really good advice. When something isn't working as you expect, you can set a breakpoint in your debugger and check your assumptions. It's almost always faster and more accurate, and you'll learn more about how the code works compared to puzzling through it. – munk Mar 09 '14 at 05:07
  • I think you're just forgetting to reset some variables at the end of your loop. Setting `check2` and `check3` to `0` where you set `check1` to `""` may resolve your issue. – Marc Baumbach Mar 09 '14 at 05:08
  • I wouldn't be here if I had gotten useful information from the debugger. All of my values reset properly, the program just decides to skip the name = input.nextLine().trim(); on subsequent passes through the loop. – Iccirrus Mar 09 '14 at 05:12
  • 2
    As soon as you enter a valid employee, your `check2` and `check3` are within a valid range and they skip the `while` loops. Resetting them at the end of your outer loop should fix that issue. In regards to the nextLine issue, just execute this at the end of your loop as well: `input.nextLine()`. This is needed because `nextInt()` doesn't read to the end of the line and that's your last read. – Marc Baumbach Mar 09 '14 at 05:22

3 Answers3

0
While (!check1.equals("9999"));

at the end while reseting values, reset check2 and check3

 check1 = "";
       name = "";
       check2=0;
       check3=0;
       age = 0;
       salary = 0;
       count++;
Adnan Ahmad Khan
  • 634
  • 2
  • 7
  • 25
  • Corrected that, changes nothing with the execution of the program. That line is just controlling the outermost loop, which is working properly. entering 9999 for the name properly exits the loop. The problem is that after the first time through the loop, it requests a name,then says the string is empty without allowing me a chance to input,then it allows me to input but the program doesnt continue from there. it just repeatedly asks me for a name until I type 9999 – Iccirrus Mar 09 '14 at 05:03
  • Let me Copy Your Code – Adnan Ahmad Khan Mar 09 '14 at 05:11
  • Please enter an employee name: An employees name can't be an empty string. Please enter an employee name: klk 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: This is the Execution Results, Check it is it working Fine? – Adnan Ahmad Khan Mar 09 '14 at 05:13
  • The issue starts with entering the information for the second employee. Something in the section asking for a name is wrong,causing it to display unexpected behaviour – Iccirrus Mar 09 '14 at 05:17
  • Yeah, I set it to reset those as well and still nothing changed. – Iccirrus Mar 09 '14 at 05:24
  • This is the output i got after reseting Please enter an employee name: jkj Please enter an age for this employee: 25 Please enter a salary for this employee: 27000 This salary is not within the allowed range(30000-120000). Please enter a salary for this employee: 32000 Please enter an employee name: 0 An employees name can't be an empty string. Please enter an employee name: klk 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: 25 Please enter a salary for this employee: – Adnan Ahmad Khan Mar 09 '14 at 05:27
0

I'm going to go ahead and say it's because you aren't resetting your other check variables (check2 and check3).

Consequently, the same check values will be used in your loop, but you still reset age and salary on each loop.

First loop
    Name loop
        check1 assigned as name is validation passes
    Age
        check2 assigned age if in range
    Salary
        check3 assigned if salary in range
    check1 = 0
    age = 0
    salary = 0
    check2 and check3 still equal previous age and salary
    leaving them valid values
Second loop
    Name loop
        check1 assigned (validation passing of course)
    Age loop
        check2 already assigned in range, next
    Salary loop
        check3 already assigned in range, next
Rinse, Repeat

Since you don't seem to want to listen to reason, I compiled this in Netbeans and output check2 and check3 after each iteration. Perhaps this will show you that this is your issue:

run:
Please enter an employee name: Ted
check2 currently equals 0
Please enter an age for this employee: 45
check3 currently equals 0
Please enter a salary for this employee: 67000
Please enter an employee name: An employees name can't be an empty string.
Please enter an employee name: Fred
check2 currently equals 45
check3 currently equals 67000
Please enter an employee name: Jane
check2 currently equals 45
check3 currently equals 67000
Please enter an employee name: Perry
check2 currently equals 45
check3 currently equals 67000
Please enter an employee name: 
SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59
  • This isnt it, the other check values are only to make sure the age and salary fall within a possible range,they have no influence on the name part of the loop – Iccirrus Mar 09 '14 at 06:21
  • @Iccirrus, it doesn't matter, they still are loop controls and they must be reset if they are to validation over and over again. – SomeShinyObject Mar 09 '14 at 12:13
-1

== tests for reference equality.

.equals() tests for value equality.

== worked in C#

user299654
  • 36
  • 3
  • Doesn't solve my problem. While that may have been wrong, correcting it changed nothing. – Iccirrus Mar 09 '14 at 05:01
  • In the code above, the only `==` is `if(name.length() == 0)`, which is obviously correct. Moreover, the only `!=` is `while(check1 != "9999")` and `check1` is `""` whenever the condition is evaluated, so changing to `while(!check1.equals("9999"))` won't fix anything. – johnchen902 Mar 09 '14 at 12:34