Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

duplicate numbers in array using sort

 
lish McDonald
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. I read the code too fast and thought I saw a collection.
 
lish McDonald
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lish McDonald
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic