• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating an array where duplicates are not allowed

 
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am beginner in Java. Presently, stuck in creating an array, where duplicates are rejected by comparing each new entry with previous entries. So far, could not get required result. kindly let me know, where I have committed mistake.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From a quick look, it seems that the code reads exactly 4 values. Is that what it should be doing? Or should it read 4 distinct values? In that case, the "i" loop should run until it has stored 4 values in the array, not until the user has entered 4 values.

Also, the "j" loop checks the entire array, even those array elements that have not had an element assigned to them yet (in which case their value will be 0).
 
Farrukh Nadeem
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your observation about 'j' corrected.
Current problems are array reads 4 values only NOT 4 distinct values, output is all ZEROS.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

farrukh nadeem wrote:Your observation about 'j' corrected.
Current problems are array reads 4 values only NOT 4 distinct values, output is all ZEROS.


Well, I'm surprised that the program actually runs at all, because it looks to me like your inner loop will run forever.

Winston

PS: If you've updated your code, why not show it to us? And in a separate post please; don't update your original one.
 
Farrukh Nadeem
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is....
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

farrukh nadeem wrote:an array, where duplicates are rejected


That would be a java.util.Set.
 
Farrukh Nadeem
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I know somewhat about SET. Actually, I want to know if I could do it in ARRAY just to get solid understanding of looping.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

farrukh nadeem wrote:Yes, I know somewhat about SET. Actually, I want to know if I could do it in ARRAY just to get solid understanding of looping.


Perfectly reasonable. However, your inner loop will still run forever, AFAICS. See if you can work out why.

Also: re-read Ulf's post carefully, because even if you get my issue corrected, you'll still need to deal with his. Big hint: when you read the second value, how many values will there be in the array?

Winston
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you ask me, you are trying to do too much at once. This project has several components that are independent of each other.

1) getting input from the user
2) checking to see if a value is in an array
3) adding a new value to an empty spot in an array.

Each of these can be done/written completely separately from the others. By separating them into methods, you can test them separately, and make sure one works without worrying about the others.
 
Farrukh Nadeem
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Perfectly reasonable. However, your inner loop will still run forever, AFAICS. See if you can work out why.



Sorry, I failed to catch your Big Hint: Don't know why the inner loop is asking inputs whereas nextInt code is outside of inner loop. Give me another hint

Regarding fred rosenberger's suggestion of Decomposition


Also worked on it. Have little issues with it. Will share after nailing this one.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

farrukh nadeem wrote:Sorry, I failed to catch your Big Hint: Don't know why the inner loop is asking inputs whereas nextInt code is outside of inner loop. Give me another hint


OK (and this is basically what Fred is saying): Deal with one thing at a time.

SO: Do you understand why your inner loop will run forever?

When you've dealt with that, then we can deal with Ulf's issue; but not before.

Hint (again): One thing at a time. And that isn't just a paradigm for debugging; you should be designing that way too - which was Fred's main point. Get your program doing one thing really well, then add to it.

Unfortunately, to do that, you need to know what it's supposed to do, and forget about how you're going to do it.

For more information, read the WhatNotHow (←click) page.

Winston
 
Farrukh Nadeem
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the culprit....
line
should be replaced with
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

farrukh nadeem wrote:I got the culprit....should be replaced with


Congratulations!

Winston
 
Farrukh Nadeem
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My current code is as follows.
current observations :
[ input ] [ output ]
[ 1 1 1 1 ] [ 1 0 0 0 ]
[ 1 1 2 2 ] [ 1 0 2 2 ]
[ 1 2 2 2 ] [ 1 2 2 2 ]
Now we have 2 issues:
1 - i counter should not be incremented if the next input is duplicate
2 - if statement is not walking through ALL the array's existing elements.
Is there any error I am missing to point out?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 9 increments i no matter whether the user entered a duplicate number or a unique one. So you need to move that "i++" to a more suitable location.

I think #2 is just a follow-on problem of #1. If you fix #1, #2 may go away by itself :-)
 
Farrukh Nadeem
Greenhorn
Posts: 29
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is still not fully functional.[ input ] [ output ]
[1 2 3 4 ] [ 1 2 3 4 ]
[1 2 3 3 ] [ 1 2 3 3 ]
[1 1 2 3 4 ] [ 1 2 3 4 ]
[ 1 1 2 2 3 ] [ 1 2 2 3 ]
Could you point out further mistakes. I am afraid I have to follow fred's instructions
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see a few issues with this code. For starters, j is never incremented - that can't be correct.

Secondly, the j loop runs up to i - but i is the index where you want to store the next unique element, so there is no valid value in array[i] at that point.

Once you've fixed these, you'll notice that the loop isn't executed for i=0 - which is correct, since the first number you enter will always go into the array (because there aren't yet other numbers to compare it to). So you'll have to think about a way to execute the instructions of the "else" branch even if the loop isn't executed at all.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

farrukh nadeem wrote:The code is still not fully functional.


I really think you're hamstringing yourself by trying to do this all in main(). You'll probably get there in the end, but then you'll (hopefully) re-read Fred's post and try to do as he says, and you'll discover that this can be done with half the code (possibly less).

A couple of big hints for you:
  • Make the array a member of your class - even a static member at this stage; although strictly speaking, it's not a good practice.
  • Add another member field that keeps track of the number of elements you've added; and give it a better name than 'j'.

  • That alone should make the whole process a lot simpler, and your code a lot more readable.

    Winston
     
    Farrukh Nadeem
    Greenhorn
    Posts: 29
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Code has been decomposed as follows although not functioning:
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    farrukh nadeem wrote:Code has been decomposed as follows although not functioning:


    OK, so do you have a question?

    The lines you've marked and the reasons given seem very clear to me.
    For example: take line 9. Do you really not understand what it is saying? Have another look at the method definition on line 18.

    However, this version looks WAY better than your first one.

    Winston
     
    fred rosenberger
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You should not be writing this much code. Pick ONE of those methods. Delete everything else (or if you insist, comment them out). Get that method to work. As you work on it, only write AT MOST 2-3 lines of code before you re-compile, test, debug, and repeat this cycle.

    ONLY once you have that method working, and being rock-solid, should you start working on the next method.
     
    Farrukh Nadeem
    Greenhorn
    Posts: 29
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Stuck at coding and closed IDE..... probably I should go to learning table again.... would you suggest something I could learn Java systemically ?
     
    Ranch Hand
    Posts: 99
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    farrukh nadeem wrote:Stuck at coding and closed IDE..... probably I should go to learning table again.... would you suggest something I could learn Java systemically ?



    Yes, the post right above yours. Why do you insist on ignoring perfectly good (and logical) advice?
     
    Farrukh Nadeem
    Greenhorn
    Posts: 29
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I looks I hurt you people out there. I regret I did not meant it. Actually, I am serious in what I asked in mt previous post, because I am not CS literate and learning at home by working on various book examples. I feel I always deviate from Winston's advice of WHATnotHOW, my mind always jumps ahead to code the problem, instead of tearing down the problem and make logic of every step to code it.

    So, I thought I should learn the "best coding practice" first by following resources available on internet.
    I hope you understand now.
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    farrukh nadeem wrote:I looks I hurt you people out there. I regret I did not meant it.


    No probs. Sometimes we feel we need to be blunt to get the message across.

    Actually, I am serious in what I asked in mt previous post, because I am not CS literate and learning at home by working on various book examples.


    All the more reason to follow Fred's advice and take things slowly and steadily. The idea is that you don't (or shouldn't) suddenly encounter a lot of problems at the same time. If you think before you code, you have a much better chance of getting it right; and if you compile and test often, you are
    (a) unlikely to get multiple errors (compilers have a nasty habit of throwing up lots of error messages that stem from the same source).
    (b) going to be pretty sure that each method is working as it should before you tackle the next one.

    I feel I always deviate from Winston's advice of WHATnotHOW, my mind always jumps ahead to code the problem, instead of tearing down the problem and make logic of every step to code it.


    It's a hard habit to break, especially when your mind is filled with Java syntax from whatever book or tutorial you're reading. And don't worry, you're not alone: I still do it myself, even after 35 years - it's just that I've learned to spot when I'm doing it, and stop.

    So, I thought I should learn the "best coding practice" first by following resources available on internet.


    Unfortunately, there's no "magic bullet".

    My best advice for right now: Before you tackle any of the problems in your book, turn your computer OFF. And don't turn it back on, or write one line of Java code until you have the entire logic for the solution written in English (or your native language).

    And then...? Follow Fred's advice.

    Winston
     
    reply
      Bookmark Topic Watch Topic
    • New Topic