Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-10-2008, 11:19 AM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
garyscott101 is on a distinguished road
Default 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();
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-10-2008, 11:46 AM
Member
 
Join Date: Dec 2008
Location: Enschede, Overijssel, the Netherlands
Posts: 19
Rep Power: 0
Sven is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-10-2008, 11:47 AM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
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?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-10-2008, 11:58 AM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
garyscott101 is on a distinguished road
Default
so what would be the code for that ?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-10-2008, 12:05 PM
Member
 
Join Date: Dec 2008
Posts: 3
Rep Power: 0
Cody is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-10-2008, 12:10 PM
Member
 
Join Date: Dec 2008
Posts: 3
Rep Power: 0
Cody is on a distinguished road
Default
Let me know when come against trouble i will do your code
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-10-2008, 12:33 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Originally Posted by garyscott101 View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-10-2008, 12:36 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Originally Posted by Cody View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 12-10-2008, 01:15 PM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
garyscott101 is on a distinguished road
Default
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 ();
            }
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 12-10-2008, 01:19 PM
Member
 
Join Date: Dec 2008
Posts: 3
Rep Power: 0
Cody is on a distinguished road
Default
U done that yet Gary, what bout your other class mates ask them?

What colleage u go to anyway Gary Scott
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 12-10-2008, 01:34 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 12-10-2008, 03:07 PM
Member
 
Join Date: Dec 2008
Posts: 4
Rep Power: 0
garyscott101 is on a distinguished road
Default
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 ();
   
            }
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 12-10-2008, 03:39 PM
CJSLMAN's Avatar
Moderator
 
Join Date: Oct 2008
Location: Mexico
Posts: 1,159
Rep Power: 3
CJSLMAN is on a distinguished road
Default 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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 12-10-2008, 03:58 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Originally Posted by CJSLMAN View Post
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.) ;-)
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 12-10-2008, 04:01 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
Default
Originally Posted by garyscott101 View Post
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing array elements Jeremy720 New To Java 2 10-13-2008 03:33 AM
comparing array using character Anseki New To Java 7 10-03-2008 08:28 PM
How to check whether two elements are available in an array? venkatteshb New To Java 8 08-27-2008 11:45 PM
please i need the code of comparing these two array lists. raj reddy JavaServer Pages (JSP) and JSTL 5 04-18-2008 08:42 AM
Help with array of elements zoe New To Java 1 07-24-2007 06:33 PM


All times are GMT +2. The time now is 05:06 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org