aspose file tools*
The moose likes Beginning Java and the fly likes duplicate numbers in array using sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "duplicate numbers in array using sort" Watch "duplicate numbers in array using sort" New topic
Author

duplicate numbers in array using sort

lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
My first post. pardon if I don't do it properly

I am having difficulty with a simple code, atleast it should be simple. I get an error that it can't find symbol : numbers.sort(); and ^ is at the period. I'm struggling. If someone can peak and give advice?

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Welcome to CodeRanch!

Sort is a static method so you have call it on Collections instead of your list:
Collections.sort(numbers).

Note that sort() doesn't eliminate the duplicates if that is what you are hoping.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Welcome to the Ranch!

It looks like you are calling sort() on an int array. An array does not have a method named sort(), so that is why you get the error. Perhaps you meant to call the
Arrays#sort(int[]) method?


Steve
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

lish dish wrote:I am having difficulty with a simple code, atleast it should be simple. I get an error that it can't find symbol : numbers.sort(); and ^ is at the period.

So, have you written a sort() method for an array? Because the designers certainly didn't.

Have a look at the Arrays class - and I warn you, it ain't over even then unless you really look hard and do a bit of thinking.

Come back when you've done that, and tell us:
(a) what the solution is
(b) you can't work it out - but if you do, please tell us what you have worked out (if anything); it saves us a lot of time.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

Good point. I read the code too fast and thought I saw a collection.
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
thank you for all your assistance. I still am having an error. I don't understand the documentation very well, but I am trying to! I added the public void sort and now it is stating that it can't find [i]^ on my input code line. Obviously I am missing something. I have a feeling that I'm suppose to do a return somewhere, since it is not a void return I am still trying to grasp. Question: should I be doing another for loop say for int j and then comparing j and i ? and putting that answer into a new array?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I doubt it's saying "can't find [ i ]." Copy/paste the exact, complete error message, and indicate clearly which line it's talking about.
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
you are correct. it was saying numbers. sorry. one of my other codes is saying i

C:\EliminateDuplicate.java:12: error: cannot find symbol
numbers[i] = input.nextInt();
^
symbol: variable i
location: class EliminateDuplicate
1 error

Tool completed with exit code 1
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Okay, so the error message is telling you exactly what's wrong. You're referring to a variable called i, but there is no such variable in that scope. At the line where you are trying to get the input, the variable i does not extist.

Also, it looks like you're intending to fill the array with user inputs, but you've only got one call to get a user input and put it into the array. You'll want to put that in a loop.
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
I've been without a computer and now have a new one Since last post, I have attempted to do more reading and have compiled the following code. I am getting an error msg at 13: out of bounds
I really need assistance. I can't tell you how discouraged I am. I know its simple, but I just can't wrap my brain around it.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I really need assistance. I can't tell you how discouraged I am. I know its simple, but I just can't wrap my brain around it.


Patience is a virtue. Desperation will lead you nowhere. Copy that. First of all a warm welcome. Looks like you are new to Java language. Will you do me a favor? Grab a copy of this book. Start studying it. Devote some time daily to understanding the fundamentals. That is where your problem lies. You are trying to get ahead of yourself. When you get a firm grasp on the concepts, you will yourself understand what the problem with your approach is. Even if we give you the answer directly here, it would be of no or little use as it would not benefit you. Hope you understand what I am trying to put through.


~ Mansukh
lish McDonald
Greenhorn

Joined: Mar 03, 2013
Posts: 12
I've heard of this book. Thank you, I wasn't sure if it was a good one or not. and I completely agree with your comment
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: duplicate numbers in array using sort