What's causing this error?

Hi!

I'm writing a simple program to sort my simple list slapped together in the same program. Unfortunately, I'm getting a compile error looking like this:

InsertionSort.java:32: cannot find symbol

symbol : method parseInt(java.lang.Object)

location:class java.lang.Integer

int positionElement = Integer.parseInt(myList.get(position));

^

Note: InsertionSort.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:uncheckedfor details.

1 error

And here's the program:

import java.util.LinkedList;

class InsertionSort{

// Main method

publicstaticvoid main(String[] args){

// Create our fancy linked list

LinkedList myList =new LinkedList();

// Let's add some arbitrary numbers to it

myList.add("13");

myList.add("5");

myList.add("18");

myList.add("7");

System.out.println("The unsorted list: " + myList);

System.out.println("The sorted list: " + myList);

// Test

int size = myList.size();

int marked = 0;

int position = 0;

for (position = 0; position < size; position++){

marked = position;

int positionElement = Integer.parseInt(myList.get(position));

while (positionElement > (positionElement + 1)){

position++;

if (positionElement > (positionElement + 1)){

myList.add(position, positionElement);

}

}

position = marked;

}

}

}

[2573 byte] By [Banangrodaa] at [2007-11-27 7:35:40]
# 1
parseInt() needs a String, not an Object. You can cast it, or make use of generics (if you are using version 1.5+).
CaptainMorgan08a at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 2
Cool, that solved the compile error (using generics). Thanks. :) Now, on to the fact that my "algorithm" didn't work as intended... :PMessage was edited by: Banangroda
Banangrodaa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 3
You might want to reconsider using add() in your algorithm implementation. It will just keep on adding elements to your ArrayList.
DarumAa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 4
Forever, like an infinite while loop?
Banangrodaa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 5

Any reason you don't use Collections.sort(myList)? You can still do integer type comparisons on the strings by using a comparator, something like this:

Collections.sort( myList, new Comparator() {

public int compareTo( Object o1, Object o2 ) {

return Integer.parseInt(o1.toString) - Integer.parseInt(o2.toString);

}

public boolean equals(Object o1, Object o2 ) {

return Integer.parseInt(o1.toString) == Integer.parseInt(o2.toString);

}

});

Jemiah

fishninja007a at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 6
Well, the reason is because this is a school assignment, and they wanted us to see how it worked a bit more than you do using collections.
Banangrodaa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 7
> Well, the reason is because this is a school> assignment, and they wanted us to see how it worked a> bit more than you do using collections.When are classes over?
Hippolytea at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 8
Tomorrow, actually, so I need to finish it... ;)
Banangrodaa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 9

Here is the C implementation. Now all you need to do is port it to java using ArrayList:

void insertionSort(int numbers[], int array_size)

{

int i, j, index;

for (i=1; i < array_size; i++)

{

index = numbers[i];

j = i;

while ((j > 0) && (numbers[j-1] > index))

{

numbers[j] = numbers[j-1];

j = j - 1;

}

numbers[j] = index;

}

}

DarumAa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 10

Well, unfortunately it has to be a Linked List (for reason unknown :/).

Anyway, this is the code as of now. However, instead of arranging the numbers in the correct order, it orders them like this: [13, 5, 5, 5, 5, 5, 5, 5, 18, 7, 4, 6, 12]. And this is completely wrong...

Here's the code:

import java.util.LinkedList;

class InsertionSort {

// Main method

public static void main(String[] args) {

// Create our fancy linked list

LinkedList<Integer> myList = new LinkedList<Integer>();

// Let's add some arbitrary numbers to it

myList.add(13);

myList.add(5);

myList.add(18);

myList.add(7);

myList.add(4);

myList.add(6);

myList.add(12);

System.out.println("The unsorted list: " + myList);

// Let's get down to business...

int size = myList.size();

int position;

int target;

if (myList != null) {

for (position = 1; position < size; position++) {

target = position;

while ((myList.get(position)) >= (myList.get(target))) {

target++;

}

myList.add(target, myList.get(position));

}

}

System.out.println("The sorted list: " + myList);

}

}

Banangrodaa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 11

It is completely wrong because you are not following the Insertion Sort Algorithm. Insertion Sort is a standard algorithm. You do not need to re-invent the wheel. Just try to understand the code I pasted in the previous reply and follow it.

Hint: You will just need get() and set() methods of a Linked List.

G'luck.

DarumAa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...
# 12
Using the C implementation as a reference, it worked out nicely. Thanks for all your help. :)
Banangrodaa at 2007-7-12 19:16:17 > top of Java-index,Java Essentials,Java Programming...