wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem with using selectionSort on number input, says cannot be applied to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with using selectionSort on number input, says cannot be applied to int" Watch "Problem with using selectionSort on number input, says cannot be applied to int" New topic
Author

Problem with using selectionSort on number input, says cannot be applied to int

Madysen Rayne
Greenhorn

Joined: Feb 03, 2004
Posts: 11
Hello everyone I am new here, so I hope I don't get a "fine" of 10 posts if this question is not proper for this forum
anyway, here is my problem. I am new to java, thus still very confused on classes methods interfaces etc etc but trying to learn it all..
I have in one folder a selectionSort I wrote in a package called utilities as Arrays.java
I made sure to put in package utilities; at the top.
the code I have written is:

etc
Now in my main program I have written to test my sort to see if it works (it is in a different folder) I have written:

My compilation error says: selectionSort(java.lang.Comparable[]) in utilities.Arrays cannot be applied to (int[])
I am confused.. I thought that when you use Comparable, that it means the input must be something that can be compared (such as ints)... can anyone explain how to fix this and why do i get this error?
Thank you )
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
A Comparable has to be an object, such as an Integer. In this code, you are passing an array of int, which is a primitive. Try passing it an array of Integers, and it should work.
Angel
Madysen Rayne
Greenhorn

Joined: Feb 03, 2004
Posts: 11
I changed the arrays to Integer... but now i get a compilation error that says:
data[i] = Integer.parseInt(line); incompatible types
Now I know data[i] is of Integer (cause I changed it) and I know the right side the parseint turns it into int... and I know int is a type of Integer, so how come this now doesn't work?
Madysen Rayne
Greenhorn

Joined: Feb 03, 2004
Posts: 11
also.. trying this:
data[i] = Integer(line);
gives me the error:
cannot resolve symbol
symbol: method Integer (java.lang.String)
location: class Testing
my logic, was that array data is Integer, and the input comes in as a String, so changing it to Integer should work...
I am confused ><
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You almost got it.
data[i] = new Integer(line);


[Jess in Action][AskingGoodQuestions]
Madysen Rayne
Greenhorn

Joined: Feb 03, 2004
Posts: 11
It compiled!!! thank you! finally after hours I got it compiled! how come I need new?
hmm now when I go to run it i get "exception in thread "main" java.lang.NoSuchMethodError: main"
but at least I got it compiled I still have huge smile on my face
Colin Kenworthy
Ranch Hand

Joined: Aug 06, 2001
Posts: 88
... and I know int is a type of Integer

int and Integer are not related, one is a primitive and the other is an Object.
Madysen Rayne
Greenhorn

Joined: Feb 03, 2004
Posts: 11
*cheers* i got it to run i needed to change from int to String in my main... to this :

when i execute, it asks for the numbers, i go to enter a number and hit enter, and i get error message- arraoutofbounds..but now I can debug and work through the logic of my sort and finally be productive again
thank you again...!
Madysen Rayne
Greenhorn

Joined: Feb 03, 2004
Posts: 11
Colin: yeah, I need to read up more on int and Integer... i am still a bit hazy on what the relationship is between the two. I didn't know there was none... although i did know int was a primitive and Integer object, I thought int was a type of Integer.. but just now goes to show you I really need to understand the real meanings behind primitive and Object, which I am still ignorant of ><
Also, need to figure out how to implement code in the correct syntax.. like for example sometimes you have something.something(); or you have something(something); those are the next to things on my list to try and master.. that is after I have this selectionsort debugged...
 
Don't get me started about those stupid light bulbs.
 
subject: Problem with using selectionSort on number input, says cannot be applied to int
 
Similar Threads
how to print a christmas tree?
I/O: searching an address book text file
Sorting a string
Runtime.getRuntime() help plzzzzzzzz
Reordering Alphabets