Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 48
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Nested loop and comparing values in array

    I dont have the clear idea.
    Yes, that is important to have before trying to write code.
    How is the code going to solve the problem?

    my code already compares all the elements in the array.
    Yes, but is it doing the compares correctly to get the desired result?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    I have tried with some boolean variables, but that didt work properly.
    My loops are comparing every element in the array as it should, so I dont see anything wrong with the comparing part, but that could just be me.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Nested loop and comparing values in array

    My loops are comparing every element in the array
    What is the code supposed to detect? What tests need to be made find it?
    At the end the code is supposed to report:
    3 all the same
    2 of the 3 are the same
    none are the same

    How can the code determine which one is correct and remember it so it can print the right results?
    It looks like it needs a counter to know if 3 or 2 or none were found.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    yes I thought it was a counter and I have tried couple of things, but that did not work out very well.

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

    Default Re: Nested loop and comparing values in array

    that did not work out very well.
    Can you explain how you were using a counter and what the problem(s) were?
    For example
    when was the counter incremented?
    when was the counter reset?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    Well the problem is I see that an counter can be helpfull i knew this before to, but its hard for me to visualize how the counter should keep on eye on how many values are equal.
    I dont see it in my head so it becomes difficult, s I tried some random stuff with counter.

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

    Default Re: Nested loop and comparing values in array

    I dont see it in my head so it becomes difficult
    That is the reason I have been suggesting that you write the steps down on paper so you can look at them. And so we can make comments and recommendations on them BEFORE you try to write any more code.

    I still write out a list of steps for more complicated parts of a program. Once I have studied the steps and am sure that they will work, I then write the steps as comments in the code and use them as a guide for writing the code.
    If you don't understand my response, don't ignore it, ask a question.

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    25

    Default Re: Nested loop and comparing values in array

    That's why I have a pad of paper next to me.
    90% of it is doodles and scribbles around code flow for whatever issue I happened to be dealing with at the time.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Nested loop and comparing values in array

    1. I compare all the elements in the array using a nested loop, which I have now and it is working.
    2. Then using 1. if-statement to check if 3 values are equal. If true then print out a text.
    3. 2. if-statement to check if only 2 of the values are equal. If true then print out a text.
    4. if the if-statements arent true then none of them are equal. print some text.

    Now if I have to solve the problem of knowing the difference of 2 elements or 3 elements, I can do that with the counter, but I dont have the idea, maybe it could
    be something like counting how many time the loop runs, I dont know.
    Last edited by musti; 02-14-2017 at 05:51 PM.

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

    Default Re: Nested loop and comparing values in array

    Step 1 needs to be expanded into all the steps that the code needs to do so it can print out one of the three lines.

    Steps 2-4 are are simple if tests of the results from step 1.

    So how will step 1 determine if there are 3 or 2 or none equal values?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    I dont know, I guess using 3 if-statements? I Seriously dont know, I wouldn't spend your time like this.

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

    Default Re: Nested loop and comparing values in array

    What would the if statements be testing the value of?

    The results of the logic for statement 1. in post#29 would be a value that said if there were 3 or 2 or no matching elements with the same value in the array.
    The if statements in statements 2. to 4. in post#29 would use the value found in the logic for statement 1. to print out the appropriate message.

    What if you were supposed to examine 3 balls laying in a row on a table and were supposed to report if all 3 were the same color, or 2 had the same color or that they all had a different color. Note: The balls' colors can only be compared when they are held in your hands.
    The steps might be:
    pick up leftmost ball in left hand
    pick up next ball to right in the right hand
    compare the colors of the balls in your hands
    if the colors match, what would you do?
    if the colors don't match, what do you do?
    next the right hand puts down the ball it is holding and picks up the next ball to the right
    and repeats the above steps
    what to do now? Did the above detect all 3 the same color? Then we're done
    What if 2 were the same color? Are we done now or do we have to look some more?
    what if the ball in the left hand did not match either of the other 2 balls? What next?
    Last edited by Norm; 02-14-2017 at 07:51 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    if(a[i] == a[j] && i!=J){
    sout("The 3 elements has the same value");
    }else if(a[i] == a[j]){
    sout("2 elements has the same value");
    }else{
    sout("no elements has the same value");
    }
    is this correct?

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Nested loop and comparing values in array

    is this correct?
    Did you compile it and execute it with different test values to see if it works as desired?

    If you don't work out the logic before writing code, you'll waste a lot of time trying to fiddle bad code trying to get it to work.

    Please remember when posting code to wrap code in code tags.

    Take a look at the end of post#32
    Last edited by Norm; 02-14-2017 at 07:20 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    My code did not work. It prints out 3 times. is it my if-statements that are wrong? I can't get my head around it. It should be easy.

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Nested loop and comparing values in array

    is it my if-statements that are wrong?
    What is the logic for the if statements you are writing?
    You need to work out the logic for your program. When you get good logic written down, we can help you write the code to implement that logic.

    See post#32 for some ideas on how to think through the problem and find a solution.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    Java Code:
    if(a[i] == a[j] && i!=j)
    this one would check if 3 of them is the same values.

    The second one should also compare all of them but only printout if 2 them is the same, the code for that i'm not sure:S

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,942
    Rep Power
    30

    Default Re: Nested loop and comparing values in array

    Please try to work out the logic for the program before trying to fiddle with bad code.

    See post#32
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    this one would check if 3 of them is the same values.
    Java Code:
    if(a[i] == a[j] && i!=j)
    How? It only compares two values.

    What is the purpose of i!=j? When can they ever be equal?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Nested loop and comparing values in array

    you would agree that this would compare all the elements?
    Java Code:
     if (a[i] == a[j])

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2013, 10: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, 10:32 AM
  3. Comparing array values with int's
    By kcon90 in forum New To Java
    Replies: 11
    Last Post: 10-29-2012, 01:45 AM
  4. Help with comparing values
    By sabarooni in forum New To Java
    Replies: 1
    Last Post: 12-06-2011, 03: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
  •