
12-10-2008, 11:19 AM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
comparing elements in array
Hi can anyone help me here,
Working on a program that puts in a Salesperson reference number in a array and want to put in a validation statement so if the user enters in the same number twice in will come up a error message ? I have one validation working in italics but cant get the other one working in bold ?
Help appreciated !!!
for (index = 0 ; index < howMany ; index ++)
{
counter ++ ;
System.out.println("What is the Salesperson reference number of employee " + counter + " ? ");
salesRef [index] = InOut.readInt();
while (( salesRef [index] <1000 ) || (salesRef [index] >9999))
{
System.out.println ("Reference number must be between 1000 and 9999");
System.out.println("Please re-enter the correct reference number ? ");
salesRef [ index] = InOut.readInt ();
}
while (salesRef [index] == salesRef [index])
{
System.out.println("Cant do");
System.out.println("Please Reenter");
salesRef [index] = InOut.readInt();
}
|
|

12-10-2008, 11:46 AM
|
|
Member
|
|
Join Date: Dec 2008
Location: Enschede, Overijssel, the Netherlands
Posts: 19
Rep Power: 0
|
|
|
Code:
|
for (index = 0 ; index < howMany ; index ++) {
counter ++ ;
System.out.println("What is the Salesperson reference number of employee " + counter + " ? ");
salesRef [index] = InOut.readInt();
while (( salesRef [index] <1000 ) || (salesRef [index] >9999))
{
System.out.println ("Reference number must be between 1000 and 9999");
System.out.println("Please re-enter the correct reference number ? ");
salesRef [ index] = InOut.readInt ();
}
while (salesRef [index] == salesRef [index])
{
System.out.println("Cant do");
System.out.println("Please Reenter");
salesRef [index] = InOut.readInt();
}
} |
Use code tags around your code next time.
|
|

12-10-2008, 11:47 AM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
Well, you're comparing it to itself, that would lead to an infinite loop.
Shouldn't you be looping through the array elements (again) up the current index, and checking the current index against those?
|
|

12-10-2008, 11:58 AM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
|
so what would be the code for that ?
|
|

12-10-2008, 12:05 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 3
Rep Power: 0
|
|
|
I think u need to set up another array and put non unique values in that array, u can set up a hash as well.
|
|

12-10-2008, 12:10 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 3
Rep Power: 0
|
|
|
Let me know when come against trouble i will do your code
|
|

12-10-2008, 12:33 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
Originally Posted by garyscott101
|
|
so what would be the code for that ?
|
Another for loop instead of a while loop. Try it, I am definately not going to do your homework for you.
|
|

12-10-2008, 12:36 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
Originally Posted by Cody
|
|
Let me know when come against trouble i will do your code
|
And how will you (not that I really think you're up to it) doing hie code help him?
Even if it is right, how will he have learned anything (which is the entire purpose behind assignments, BTW)?
He will cut-n-paste it, if possible, turn it in, and that's it. He will still have no idea what he turned in, nor how it worked, nor why it was done that way. So, next week, when he gets a new assignment that is meant to build upon what he learned by doing this one, he will be even more lost than he his here.
|
|

12-10-2008, 01:15 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
I thought this seemed logical to me but didnt work
|
Code:
|
for (index = 0 ; index < howMany ; index ++)
{
counter ++ ;
System.out.println("What is the Salesperson reference number of employee " + counter + " ? ");
salesRef [index] = InOut.readInt();
for (index = 1 ; index < howMany ; index ++)
{
if (salesRef [index] == salesRef [index])
{
System.out.println ("Already been entered.");
}
}
while (( salesRef [index] <1000 ) || (salesRef [index] >9999)) // value must be between 1000 and 9999
{
System.out.println ("Reference number must be between 1000 and 9999");
System.out.println("Please re-enter the correct reference number ? ");
salesRef [ index] = InOut.readInt ();
} |
|
|

12-10-2008, 01:19 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 3
Rep Power: 0
|
|
|
U done that yet Gary, what bout your other class mates ask them?
What colleage u go to anyway Gary Scott
|
|

12-10-2008, 01:34 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
|
Code:
|
salesRef [index] == salesRef [index] |
you are still comparing the same element to itself, however.
First of all move the second for loop down inside the while loop directly after the read statement (as it doesn't do any good to check the "current" indexes value when either 1 it doesn't have one yet, or two, it doesn't have a valid value) and then change the value back to 0 when it is found to be a repeat (or the while loop condition may still consider it valid).
Second, don't "reuse" the index variable, change "int index" in the second loop to "int whateverYouWant". Using index in the inner loop, will "hide" the index variable that is contained in the outer loop.
Then there is no reason for the second loop to loop all the way to the of the array, is there? Looping to the index that is "currently in question" (to put it that way), is far enough, is it not? So change "howMany" to "index" in the inner loop.
Edit: best would be to turn that for loop into a method of it's own so that a boolean result could be delivered that could then be included as part of the while loop condition statement.
|
|

12-10-2008, 03:07 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 4
Rep Power: 0
|
|
got it finally created a new loop and while statement thanks for your help
// Input employees reference number
|
Code:
|
for (index = 0 ; index < howMany ; index ++)
{
counter ++ ;
System.out.println("What is the Salesperson reference number of employee " + counter + " ? ");
salesRef [index] = InOut.readInt();
// check for valid reference number
for (index1 = 0 ; index1 < index ; index1 ++)
{
while (salesRef[index1] == salesRef[index])
{
System.out.println("Sorry this Sales Reference number has already been used , Please Re-enter");
salesRef [index] = InOut.readInt();
}
}
while (( salesRef [index] <1000 ) || (salesRef [index] >9999)) // value must be between 1000 and 9999
{
System.out.println ("Reference number must be between 1000 and 9999");
System.out.println("Please re-enter the correct reference number ? ");
salesRef [ index] = InOut.readInt ();
} |
|
|

12-10-2008, 03:39 PM
|
 |
Moderator
|
|
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
|
|
Maybe I'm confused
According to the text...
|
Code:
|
System.out.println ("Reference number must be between 1000 and 9999"); |
But the logic seems to point in another direction
|
Code:
|
while (( salesRef [index] <1000 ) || (salesRef [index] >9999)) |
The above is saying "When the ref number is less than 1000 or greater than 9999". Not sure that is the correct logic you're looking for.
Luck,
CJSL
__________________
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
|
|

12-10-2008, 03:58 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
Originally Posted by CJSLMAN
|
According to the text...
|
Code:
|
System.out.println ("Reference number must be between 1000 and 9999"); |
But the logic seems to point in another direction
|
Code:
|
while (( salesRef [index] <1000 ) || (salesRef [index] >9999)) |
The above is saying "When the ref number is less than 1000 or greater than 9999". Not sure that is the correct logic you're looking for.
Luck,
CJSL
|
Well, inside the while loop he asks for the user reenter a value, so the logic is correct. (A "negative" check with "positive" values.) ;-)
|
|

12-10-2008, 04:01 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
|
|
Originally Posted by garyscott101
|
|
got it finally created a new loop and while statement thanks for your help
|
Much closer, but now you check that the number doesn't repeat, then you start checking whther or not it's in the proper range. Both of these in separate loops, so what is to prevent the user from entering a unique value that is outside the range (say 5), then entering, in the second loop, a number that is a repeat?
Like I say, you need to check all the conditions in the while loop conditions (this one last so it doesn't loop unless it needs to). You can do that by putting this second loop into a method that returns a boolean, and calling that method as a while loop condition.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 05:06 PM.
|
|