Page 1 of 2 12 LastLast
Results 1 to 20 of 29
  1. #1
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Angry can't leave while loop when verifying strings

    Ok, this is a very miniscule part of a large project and I cannot seem to get this part to work right. Eventually this option (choice 1) will create new employees using arrayList, but right now I am just trying to get it to verify that either "salaried" or "hourly" is entered. otherwise it should keep in loop. no matter what, it will not exit the loop even if one of the two correct options are entered. OH, and "empType" is just a string that asks which type of employee you want to create.
    Java Code:
    .....
    // create employee, hourly or salaried?
    else if (choice == 1)
    {
           String type;
           System.out.println(empType);
           scan.next();
           type = scan.nextLine();
    				
           while((!type.equalsIgnoreCase("hourly")) && (!type.equalsIgnoreCase("salaried")))
    	{
    				
    		System.out.println("***Please enter either 'salaried' or 'hourly'.***");
    		System.out.println(empType);
    		scan.next();
    		type = scan.nextLine();
    	}
    			
    }
    			
    //set pay for an employee
    else if (choice == 2)
    {
    .......

    this is due tonight and this stupid little detail has had me hung up for hours. I have 4 other full classes including an interface done and this one little detail in the functional class is killing me, I know its something stupid. Thanks ahead of time

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    "either "salaried" or "hourly" is entered" so why are you using && instead of ||?

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why not try something like
    Java Code:
    while(true){
      if(type.equalsIgnoreCase("salaried") || type.equalsIgnoreCase("hourly")){
        break;
      else{
        report error and re prompt
      }
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by r035198x View Post
    "either "salaried" or "hourly" is entered" so why are you using && instead of ||?
    Perhaps I am just having a retard moment but I feel like this wouldn't work, if I am wrong please correct me. He only wants to enter the loop if the type is not salaried and not hourly, so if he has type hourly he wants to skip the loop, same with when the type is salaried, would the or still be the correct change?

    I believe my solution above is easier, however; id like to figure this of. My mind is flip flopping on this and I know it's simple as well.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by sunde887 View Post
    ..He only wants to enter the loop ..
    The problem is not that he can't enter the loop.
    The problem is that he can't leave the loop.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    @OP, why do you have the lines
    Java Code:
    scan.next();
    type = scan.nextLine();
    Why are you scanning twice?

    Java Code:
    while (!(type.equalsIgnoreCase("hourly") || type.equalsIgnoreCase("salaried"))) {
    	    System.out.println("***Please enter either 'salaried' or 'hourly'.***");
    	    type = scan.nextLine();
    
    }

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I believe the double scan could be causing a problem, with the original and statement ifyou type salaried or hourly the condition will not be met.

    Won't using or cause an infinite loop once inside?

  8. #8
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    the double scan is due to the while loop. so it scans for input,..... while it does not contain the right thing, it requests again, scans again, updates variable, then tries while loop again and keeps until it gets updated to either salaried or hourly.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try removing the first scan and see what happens

  10. #10
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    @Sunde887 used variation of your "easier method" above.... WAY easier! works great now thanks a lot. Although, mine should work and cannot figure out what little detail was off and is still getting to me, but the important thing is IT WORKS NOW. stay tuned, I may need a little more help with other little nuances, due in 3.5 hours. ugh

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    If you printed out the value of type it should have become clear that it never held the value of "hourly" or "salaried". In fact it held an empty string. As pointed out the error was the extra scan.
    Java Code:
    scan.next();  // scans "hourly" and throws it away
    type = scan.nextLine(); // scans an empty string

  12. #12
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    I learned most of this a year and a half ago, but haven't used java in that long so forgetting a lot of things from previous college and it always seems like the results you get from google searches, etc. never quite help as much as a little current knowledge. ok, here goes another question, not asking for you to do work for me, i do need a kick though. I have an HourlyEmployee class and SalariedEmployee class along with this main payroll class that contains the main method. I can do the separate loops depending on which choice was picked. I have to create an ArrayList to store all employees created as the whole program loops through, how do I go about creating this arraylist in my functional class and can I store both different types of employees in one arraylist or do I have to create separate ones? My constructors in the salaried and hourly classes are as follows:

    public Salaried (String _name, String _socSecNum, double _pay)
    {
    name = _name;
    socSecNum = _socSecNum;
    pay = _pay;
    }

    and:

    public Hourly ( String _name, String _socSecNum, double _pay)
    {
    name = _name;
    socSecNum = _socSecNum;
    pay = _pay;


    }

  13. #13
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    oh ok, I see what you mean now @Junky. I felt like it wasnt checking anything when in the loop so it wouldn't let me out, just didn't see why. thanks

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Have your HourlyEmployee and SalariedEmployee classes extend another class (eg Employee). You then make your array/List hold Employee objects

  15. #15
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    oh, forgot to mention, I have an abstract class "Company" I was thinking along the lines you are speaking of. the HourlyEmployee and SalariedEmployee classes implement the Company class. How does the format for this go? I can create an arrayList, but how do you create an ArrayList of two different class objects like this? I don't know how to format such an arguement

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    That does not makes sense. An HourlyEmployee is not a Company.

  17. #17
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    basically in the Payroll class I need to create an array that holds both emp types. if they choose choice 1 "create new" , as seen before they choose type. then if salaried i use the salaried constructor(from salaried class) and store in Arraylist. if hourly, use hourly constructor(from hourly class) and store in same arrayList. Later user can pull the different employees from the array list to edit/get data by the location in the array. I can do all that, but I just don't know how to initially set up the array in this functional Payroll class that will be holding this data

  18. #18
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    I had to build all classes from scratch, we were given the class names and some of what was expected to be included. but I don't think I can create any extra classes other than what were stated, which is the only reason I was trying to use the company class in that way. But basically the payroll class needs to have the arrayList that is storing the employees.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I have already answered your question. If there is something about my answer you do not understand then explain what you don't understand. Don't ignore it and keep asking the same question in the hope that you will get an answer you can understand.

  20. #20
    PaulM is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    I was trying to explain better what I need to do because I didn't think I was very clear. I know how to create an arraylist of ints or strings, etc. but what is the format for creating one of objects? and then how do I pass two different employee types to that same arraylist? obviously I need to use the constructors I mentioned above, but I can't find anything on how the format of it goes

Page 1 of 2 12 LastLast

Similar Threads

  1. Verifying of Month
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 2
    Last Post: 11-02-2010, 05:34 PM
  2. Replies: 0
    Last Post: 05-19-2010, 06:38 PM
  3. Replies: 15
    Last Post: 11-06-2009, 11:31 AM
  4. While loop comparing strings from user
    By N3VRMND in forum New To Java
    Replies: 5
    Last Post: 10-30-2009, 09:18 AM
  5. Verifying existence of a table in a db
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-14-2008, 10:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •