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

    Default Re: Nested loop and comparing values in array

    Now this code would compare and only print out if all the values are the same.

    Java Code:
    for (int i = 0; i < a.length; i++) {
                for(int j = i + 1; j < a.length-1; j++) {
                   if(a[i] == a[j]){
                       System.out.println("all equals");
                   }
                }
            }

  2. #42
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Nested loop and comparing values in array

    Ok, I'm giving up. If you don't want to design the program before trying to write the code after all the times I have suggested that you need a design before trying to write code, then there isn't anything more I'm going to do.
    Maybe if you continue to change your code, eventually it will do what you want.

    Good luck.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    Sorry can't figure this out, I have tried so many times now. Right now i'm in a bad spiral it's only getting worse with my code.
    Last edited by musti; 02-14-2017 at 10:24 PM.

  4. #44
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: Nested loop and comparing values in array

    Earlier you wrote out a very high level set of steps.

    Quote Originally Posted by musti View Post
    1. The loops compares all the values with each other.

    2. If 2 element is equal then it will be printed in the console.
    if not equal then some text will printed in the console.
    These actually come down to:
    1. Compare values
    2. Show result

    Now, forgetting entirely about part 1, what do you need to do part 2?
    What do you need to know to be able to print "all equal", or "two equal" or "none equal"?

    Once you know that, then you can write that bit of code, as a method with whatever parameters you have decided you need to do the job. Again, forgetting entirely about how those parameter(s) are created.

    Once you have that, you now know what part 1 needs to give you to do part 2.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Nested loop and comparing values in array

    Well I ned to now how MANY of them are equal or not. Right?

  6. #46
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: Nested loop and comparing values in array

    OK, so you need a count.
    So can you write a method that takes a count (an int) and displays the correct text based on that?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Nested loop and comparing values in array

    Sorry for the late answer I had lot of homework I had to take care of, I will try to make it work with the counter.

  8. #48
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: Nested loop and comparing values in array

    No.
    Don't try and make it all work.
    Just do a method that takes a count and prints out the correct output based on that.

    Until you have that, there is absolutely no point doing the part that gets you the count. Make sure this part of your code works first.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 3 of 3 FirstFirst 123

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, 11: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
  •