• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 )
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ><
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You almost got it.
data[i] = new Integer(line);
 
Madysen Rayne
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

... 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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Right! We're on it! Let's get to work tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic