Page 1 of 3 123 LastLast
Results 1 to 20 of 48
  1. #1
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Nested loop and comparing values in array

    Hello i'm new in this website, also have been programming in couple of months now.

    I have this problem i just can't solve, i have an array with 3 values and i what I want is:

    1. if all values are equal
    2. or 2 of the values are equal.
    3. or none of them are equal.

    I want to do this with a nested loop and an array not an array list, I know this can easily done without nested loops but i dont want that.

    The problem I have is if all values are equal then 2 of them also are equal, i hope you understand what i mean.

    Java Code:
        public static void compareValues(){
            int[] a = {1,1,3};
            
            for (int i = 0; i < a.length; i++) {
                for (int j = i + 1 ; j < a.length; j++) {
                    if(a[i] == a[j] && i!=j){
                    System.out.println("2 Same values: " + a[i]);
                    System.exit(0);
               }else if(/* what here?*/){
                        System.out.println("all values are equal");
                        System.exit(0);
               }else{
                        System.out.println("none of them are equal");
                        System.exit(0);
               }
            }
          }
        }
    Last edited by musti; 02-13-2017 at 11:47 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    Sounds like you need to work out the algorithm before trying to write any more code.
    What are the steps the code needs to take to solve your problem? Can you list those steps?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    First i have compare all the values and se if all of them are equal. that I have done.
    Then i need to figure out how compare all of them but only see if 2 of them are equal. this one i'm not sure.
    If all of them is equal then 2 of them is equal aswell. This confuses me.

  4. #4
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    if(a[0] == a[i] || a[1] == a[i]){
    sout("2 values are equal");
    }
    but this does not work.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    You list of steps doesn't describe how you are counting the values for equals.

    If all of them is equal then 2 of them is equal as well.
    Yes, that would be implied.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    You list of steps doesn't describe how you are counting the values for equals.
    Not sure what you want me to do?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    Describe how to solve the problem in words, not by writing code.
    How would you determine if the array has all 3 elements the same?
    How would you determine if the array had 2 that were equal?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    Compare all the elements in the array see using the loops, and then see if the elements are equal.
    Again compare the elements to see if 2 of the elements are equals.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    Compare all the elements in the array see using the loops, and then see if the elements are equal.
    That says what you want the code to do. It does not say how the code is going to do it.
    How does that code actually check if the elements are equal?
    How many elements are checked at one time? 2 or 3?
    When is the first element checked against the 2nd element? and 1st vs 3rd and 2nd vs 3rd
    What does the code do if 1st equals the second?
    What does the code do if 1st does not equal the second?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,904
    Rep Power
    10

    Default Re: Nested loop and comparing values in array

    Just curious, why are you doing this?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    Well because of the loops the first element is compared to the other elements then the second element is compared to first and the last
    then the last element is compared to the first and the second.
    Lets say the first element is equal to the second then then my code should somehow notify that, maybe the problem is here.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    the first element is compared to the other elements
    then the second element is compared to first and the last
    What happens as a result of each compare?
    Why would the second element be compared to the first element? That comparison has already been made. Wasn't it the first comparison that was made?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    For each compare then my code print's some text which it should not.
    the j = i + 1; I have in my second loop makes sure that I dont compare the elements twice.

  14. #14
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    Just curious, why are you doing this?
    Because it is more fun with a nested loop. Maybe better code?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    You still have not answered any of my questions and described how the program will determine the matches.
    For example:
    What happens as a result of each comparison between two values?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    They get compared and printed out in the console?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    They get compared and printed out in the console?
    Yes that is a high level description of what the program is supposed to do.
    The question I keep asking is: How is the program going to do it?

    Can you give more details on that?
    How are each of the values compared?
    What if they are equal?
    What if they are not equal?
    What determines what is printed?
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    The loops compares all the values with each other.
    If 2 element is equal then it will be printed in the console.
    if not equal then some text will printed in the console.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,716
    Rep Power
    29

    Default Re: Nested loop and comparing values in array

    The loops compares all the values with each other.
    Yes, again, that is what the program needs to do.
    The question is:
    What are the steps the code needs to take to compare all the values?

    How does the program keep track of the results so it knows which of the three messages it should print?
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    musti is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Nested loop and comparing values in array

    Well my code already compares all the elements in the array.

    Yes that is what I dont now, I thought maybe with a counter that did not work because I dont have the clear idea.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2013, 11:47 PM
  2. [SOLVED] Nested loop in 2D array stores same value across rows
    By Nicias in forum New To Java
    Replies: 5
    Last Post: 11-06-2012, 11:32 AM
  3. Comparing array values with int's
    By kcon90 in forum New To Java
    Replies: 11
    Last Post: 10-29-2012, 02:45 AM
  4. Help with comparing values
    By sabarooni in forum New To Java
    Replies: 1
    Last Post: 12-06-2011, 04:41 PM
  5. Loop through array and check values...
    By Spinalkord in forum New To Java
    Replies: 1
    Last Post: 10-24-2010, 10:17 AM

Tags for this Thread

Posting Permissions

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