user2803053
user2803053

Reputation: 59

I am receiving an error about incompatible type: Sort cannot be converted to java.util.arraylist<java.lang.Integer>

In my main method I am receiving the error message incompatible type. I have created a printList method to print the output. I believe the problem has something to do with this method but so far I can not figure it out.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


public class Sort 
{
/*  fun printList list =
              let
                val counter = ref 0
                val temp = ref ""
                val n = length(list)
              in
                while (!counter < n) do 
                  ( temp := !temp ^ (Int.toString(List.nth(list,!counter))^" ");
                  counter := !counter+1);
                temp := !temp ^ "\n";
                !temp 
              end;
*/

    public static String printList (ArrayList<Integer> left)
    {
        int counter = 0;
        String temp = "";
        int n = left.size();

        while(counter < n)
        {
            temp += left.get(counter)+ " ";
            counter += 1;

        }

        temp += "\n";

        return temp;

    }

/*

            fun lineList file =
              let
                val instr = TextIO.openIn file
                val str   = TextIO.input instr
              in 
                String.tokens (fn x => x = #",")str
              before
                TextIO.closeIn instr
              end;

*/

    public ArrayList<Integer> lineList(String file) throws FileNotFoundException
    {
        ArrayList<Integer> tmp = new ArrayList();
        try {
            Scanner scanner = new Scanner(new File(file));
            scanner.useDelimiter(",");
            int i = 0;
            while (scanner.hasNextInt()) {
                tmp.add(scanner.nextInt());
                i++;
            }
            scanner.close();

            } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return tmp;
    }

/*

            fun halve nil = (nil, nil)  
            |   halve [a] = ([a], nil)
            |   halve (a :: b :: cs) =
                  let
                    val (x, y) = halve cs
                  in
                    (a :: x, b :: y)
                  end;
*/
    public static ArrayList<Integer> halve(ArrayList<Integer> l, String subL){
        int halveSize = l.size() / 2;
        ArrayList<Integer> halfList = new ArrayList<Integer>(halveSize);
        if(subL == "left"){
            for(int i = 0; i < l.size(); i += 2){
                halfList.add(l.get(i));
            }
        }else if(subL == "right"){
            for(int i = 1; i < l.size(); i += 2){
                halfList.add(l.get(i));
            }
        }
        return halfList;
    }

/*
            fun merge (nil, ys) = ys
            |   merge (xs, nil) = xs
            |   merge (x :: xs, y :: ys) =
                  if (x > y) then x :: merge(xs, y :: ys)
                  else y :: merge(x :: xs, ys);

*/

     public static ArrayList<Integer> merge(ArrayList<Integer> le, ArrayList<Integer> r) {
         ArrayList<Integer> result = new ArrayList<Integer>();
         int i = 0;
         int j = 0;
         while (i < le.size() && j < r.size()) {
             if (le.get(i) <= r.get(j)) {
                 result.add(le.get(i));
                 i++;
             }else {
                 result.add(r.get(j));
                 j++;
             }

         }
         for (int k=0;k<le.size();k++) {
             if (k>i) {
                 result.add(le.get(k));
             }
         }
         for (int l=0;l<r.size();l++) {
             if (l>j) {
                 result.add(r.get(l));
             }
         }
         return result;
     }




/*

            fun mergeSort nil = nil
            |   mergeSort [a] = [a]
            |   mergeSort theList =
                  let
                    val (x, y) = halve theList
                  in
                    print("xList: "^printList(x));
                    print("yList: "^printList(y));
                    merge(mergeSort x, mergeSort y)
                  end;

*/

     public static ArrayList<Integer> mergeSort(ArrayList<Integer> l){
        if(l.size() <= 1){
            return l;
        }
        ArrayList<Integer> xList = halve(l, "left");
        ArrayList<Integer> yList = halve(l, "right");
        System.out.print("xList: " +printList(xList));
        System.out.print("yList: " +printList(yList));
        return merge(mergeSort(xList), mergeSort(yList));
    }

/*
            fun quicksort nil = nil
            |   quicksort (pivot :: rest) =
                  let
                    fun split(nil) = (nil,nil)
                    |   split(x :: xs) =
                          let
                            val (below, above) = split(xs)
                          in
                            if x < pivot then (x :: below, above) 
                            else (below, x :: above)
                          end;
                    val (below, above) = split(rest)
                  in
                    quicksort below @ [pivot] @ quicksort above
                  end;

            val optList = map (fn str => (Int.fromString str)) (lineList "/home/chella/Documents/SMLCode1/input.txt");
            val intList = map Option.valOf optList;

            fun uselessMachine x = mergeSort(quicksort(x));

            uselessMachine(intList);

*/
    public static ArrayList<Integer> quickSort(ArrayList<Integer> input)
    {
        if(input.size() <= 1){
            return input;
        }

        int centre = input.get(0);

        ArrayList<Integer> a = new ArrayList<Integer>();
        ArrayList<Integer> b = new ArrayList<Integer>();

        for(int i = 1; i < input.size(); i++){
            if(input.get(i) < centre){
                a.add(input.get(i));
            }else{
                b.add(input.get(i));
            }
        }

        ArrayList<Integer> output = quickSort(a);
        output.add(centre);
        output.addAll(quickSort(b));

        return output;
    }

    public static void main(String[] args)
    {
        //Sort <Integer>s= new Sort<Integer>();

        Sort s= new Sort ();
        //int [] a = {1,2,3,4,8,10};
        //System.out.println(s.printList(a));

        try {
            s.mergeSort(s.quickSort(s.lineList("input.txt")));
        System.out.println(printList(s)); 
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        }

    }
}

Upvotes: 0

Views: 1471

Answers (1)

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

Well the error message is pretty clear...

Sort cannot be converted to java.util.arraylist

The problem is at this line :

System.out.println(printList(s));

s is an instance of Sort according to this initialization

Sort s = new Sort();

While the signature of printList is

public static String printList (ArrayList<Integer> left)

Which is suppose to receive an ArrayList<Integer> not a Sort object.

I think what you want to use is

System.out.println(printList(s.lineList("input.txt"))); 

This will fix the error.

Edit : Here is the snippet I was talking about in comments :

Sort s = new Sort();
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
a.add(4);
a.add(8);
a.add(10);

System.out.println(printList(a));

Also if you want to print it in reverse order, then you will have to reverse the list using Collections package in the printList method which would now look like this :

public static String printList (ArrayList<Integer> left)
{
    int counter = 0;
    String temp = "";
    int n = left.size();
    Collections.reverse(left);

    while(counter < n)
    {
        temp += left.get(counter)+ " ";
        counter += 1;

    }

    temp += "\n";

    return temp;

}

the output would then be 10 8 4 3 2 1

Upvotes: 2

Related Questions