Results 1 to 15 of 15
  1. #1
    garyscott101 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    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();
    }

  2. #2
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    Java 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.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    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?

  4. #4
    garyscott101 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    so what would be the code for that ?

  5. #5
    Cody is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    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.

  6. #6
    Cody is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    Let me know when come against trouble i will do your code

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote 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.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote 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.

  9. #9
    garyscott101 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    I thought this seemed logical to me but didnt work

    Java 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 ();
                }

  10. #10
    Cody is offline Member
    Join Date
    Dec 2008
    Posts
    3
    Rep Power
    0

    Default

    U done that yet Gary, what bout your other class mates ask them?

    What colleage u go to anyway Gary Scott

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Java 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. #12
    garyscott101 is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    got it finally created a new loop and while statement thanks for your help

    // Input employees reference number

    Java 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 ();
       
                }

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Maybe I'm confused

    According to the text...
    Java Code:
    System.out.println ("Reference number must be between 1000 and 9999");
    But the logic seems to point in another direction
    Java 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.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by CJSLMAN View Post
    According to the text...
    Java Code:
    System.out.println ("Reference number must be between 1000 and 9999");
    But the logic seems to point in another direction
    Java 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.) ;-)

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote 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.

Similar Threads

  1. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  2. comparing array using character
    By Anseki in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 07:28 PM
  3. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  4. please i need the code of comparing these two array lists.
    By raj reddy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 04-18-2008, 07:42 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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