Results 1 to 20 of 29
- 03-23-2011, 08:08 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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
- 03-23-2011, 08:13 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
"either "salaried" or "hourly" is entered" so why are you using && instead of ||?
- 03-23-2011, 08:22 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Why not try something like
Java Code:while(true){ if(type.equalsIgnoreCase("salaried") || type.equalsIgnoreCase("hourly")){ break; else{ report error and re prompt } }
- 03-23-2011, 08:31 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-23-2011, 08:35 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 03-23-2011, 08:46 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
@OP, why do you have the lines
Why are you scanning twice?Java Code:scan.next(); type = scan.nextLine();
Java Code:while (!(type.equalsIgnoreCase("hourly") || type.equalsIgnoreCase("salaried"))) { System.out.println("***Please enter either 'salaried' or 'hourly'.***"); type = scan.nextLine(); }
- 03-23-2011, 08:49 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 03-23-2011, 05:59 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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.
- 03-23-2011, 06:36 PM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Try removing the first scan and see what happens
- 03-24-2011, 12:12 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
@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
- 03-24-2011, 12:19 AM #11
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
- 03-24-2011, 12:30 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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;
}
- 03-24-2011, 12:35 AM #13
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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
- 03-24-2011, 12:35 AM #14
Have your HourlyEmployee and SalariedEmployee classes extend another class (eg Employee). You then make your array/List hold Employee objects
- 03-24-2011, 12:44 AM #15
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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
- 03-24-2011, 12:48 AM #16
That does not makes sense. An HourlyEmployee is not a Company.
- 03-24-2011, 12:52 AM #17
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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
- 03-24-2011, 12:56 AM #18
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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.
- 03-24-2011, 12:56 AM #19
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.
- 03-24-2011, 01:21 AM #20
Member
- Join Date
- Mar 2011
- Posts
- 15
- Rep Power
- 0
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
Similar Threads
-
Verifying of Month
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 2Last Post: 11-02-2010, 04:34 PM -
Client cannot retrieve all strings from the InputStreamReader through a loop
By yadav in forum NetworkingReplies: 0Last Post: 05-19-2010, 05:38 PM -
Need help writing program to compare 2 strings using a loop
By kornwheat in forum New To JavaReplies: 15Last Post: 11-06-2009, 10:31 AM -
While loop comparing strings from user
By N3VRMND in forum New To JavaReplies: 5Last Post: 10-30-2009, 08:18 AM -
Verifying existence of a table in a db
By Java Tip in forum Java TipReplies: 0Last Post: 02-14-2008, 09:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks