• Post Reply Bookmark Topic Watch Topic
  • New Topic

same ints within an array of ints enter by command line args, how it works explanation please  RSS feed

 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone, my first post woo!
In lecture we was given the task to allow the user, to enter any number of ints in the command line, and find if any of the numbers entered are duplicates/repeats, if so respond false, if all unique respond true.
The args to be entered are "1, 2, 3, 4, 5, 6, 7, 8, 9, 10," (True)   ...     "1, 2, 3, 4, 5, 6, 7, 1, 2, 3," (False)    ...     "0, 1, 2, 3, 4, 0" (false)
So after many attempts and some guidance i managed to get to this. I was certain it would work and still unsure why it does not.




the issue is this only ever prints true regardless of the args entered

at the end the lecturer helped me come to this solution


Could some one please help me understand how this works? because I don't get why it does (and why mine doesn't)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Unfortunately, in taking a very quick look, neither yours or your instructor's versions should work.

Hint: What should happen when the third parameter is compared to the third parameter? Is that a duplicate?

Henry
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure what you mean i have changed the "L"'s to "as" for clarity so they aren't mixed up with 1's
and it does work! but I cant figure it out.
Apparently, its something to do with it forming a matrix and if the numbers are all different then this would cause c to go up by 1 each time there was a match, (i guess it would match itself?)
and if there was more than one duplicate it would increase the value of c more...
Since c starts as 0, plus one to it to make it equal to the array size if there was only an equivalent number of matches. if there was to many this number would be too high i guess :/
still a bit fuzzy, however writing out (badly) in my own words, what he tried explaining to me, has made it a little clearer and i think i get the gist of what might be going on here...

although could you be a little less cryptic? i don't quite get the hint

 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

My first thoughts were, like Henry's, that the lecturers version wouldn't work but then you helpfully changed the 'l' to 'as' which cleared up the problem of seeing 'L' as '1'.

It works because you are comparing every number with every number (including itself) and so if there are no duplicates there will be exactly 'as' matches (ie 1 match for every element in the array because every element only matches itself).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stef Aucky wrote:
although could you be a little less cryptic? i don't quite get the hint


Okay, your parameters are "1 2 3 4 5 6 7 8 9 10". What is the third parameter? Well... it is "3".

So, when you compare the third parameter with the third parameter, ie. comparing "3" with "3", is the result a duplicate or not?

Henry


[edit: of course, this followup hint is moot, as Tony already explained it.]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
My first thoughts were, like Henry's, that the lecturers version wouldn't work but then you helpfully changed the 'l' to 'as' which cleared up the problem of seeing 'L' as '1'.


Good point. I too, saw a one instead of a letter L.

Henry
 
Stan Austin
Ranch Hand
Posts: 50
1
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Welcome to the Ranch.

My first thoughts were, like Henry's, that the lecturers version wouldn't work but then you helpfully changed the 'l' to 'as' which cleared up the problem of seeing 'L' as '1'.

It works because you are comparing every number with every number (including itself) and so if there are no duplicates there will be exactly 'as' matches (ie 1 match for every element in the array because every element only matches itself).


Thank you hopefully I will have a great time as part of the community here,
also Noted L's make to bad/confusing names.

I did kind of get there with my own explanation, is there another way of doing this without setting up a second "duplicate array" as this was the only way i could find, my lecturer modified my logic, to work , but my version looks extremely different to the other students in my class  theirs was longer and i think used a single array)
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as the code is clear, shorter is often better.  But there are often a lot of ways to do the same task.  The first thing that came to my mind was to use maps, but I don't know if that would be "better".
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My first thought would be to adjust the inner loop. There is no reason to start at zero. It can start at "i+1" instead. Checking the case where j equals i is wrong; And checking the cases where j is less than i is not needed, as they were checked with previous iterations.

My second thought would be to not use a counter. When a duplicate is found, the task is done. Why continue checking? Just break out of the loops*.

Henry

* of course, this second suggestion may open a can of worms, as we want to break out of two loops.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for illustration, here's my way of solving the problem:

Notice that it's longer (more statements).  It doesn't use arrays at all.  It will work with any input on the command line, not just integers.  It probably uses more memory.  It might be faster.  So "better"?  Not really -- except that mine uses descriptive variable names.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can stop when you find the first duplicate. Here's another way:

Output:

Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
True

Input: 1, 2, 3, 4, 5, 6, 7, 1, 2, 3
Duplicate found: 1 at index 0 and 7
False
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute's solution looks a lot sexier though
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute's solution seems like a bit overkill in my opinion, as a one-liner is sufficient (at least if you can use Java8):
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobias, the overachiever. A cow for the one-liner!
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Just break out of the loops*.

Henry

* of course, this second suggestion may open a can of worms, as we want to break out of two loops.

No shame in doing that in a small, controlled environment. I beg to differ with whoever says otherwise.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stef Aucky wrote:. . . any number of ints in the command line, . . . "1, 2, 3, 4, 5, 6, 7, 8, 9, 10," . . .
That isn't how you enter command‑line arguments. You simply write
java MyApp 1 2 3 4 5 6 7 8 9 10
Just the arguments. No quote marks (unless you need to join two words into one argument), no commas, no nothing.
Don't use numbers instead of booleans.
boolean b = x == y;
or boolean b = (x == y); Round brackets optional.
And once you have that boolean, don't attempt == true or == false. Later on, find out about the ?: operator:-
System.out.println(b ? "Unique" : "Repeated"); or something like that.
Declare your loop indices inside the loop, as in the Java™ Tutorials. Don't use a separate variable for the size of the array; always use myArray.length.

I see you have got lots of other replies, and got your app sorted out.
And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . No shame in doing that in a small, controlled environment. I beg to differ with whoever says otherwise.
You s‍hould have anaesthetised me first, then Unless you get into bad habits and think you can do the same in a large wild environment. You can always include a flag as part of the loop header:-If you have nested loops, you would put && notYetFound in both their headers. I will ackowledge that here strict adherence to structured programming does look inelegant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!