0

I have a problem with return an array.

package iterators;

import java.util.*;
import java.io.*;


public class Nic {
    String[] abel() throws IOException {
        int i = 0;
        BufferedReader wczytaj = new BufferedReader(new FileReader("filmy.txt"));
        StringBuilder sb = new StringBuilder();
        String tekst = wczytaj.readLine();
        while (tekst != null) {
            sb.append(tekst);
            sb.append("\n");
            tekst = wczytaj.readLine();
        }
        String calosc = sb.toString();
        String film = calosc;
        String znak = ",";
        String[] tab;
        tab = film.split(znak);
        for (i = 0; i < tab.length; i++) {
            System.out.println("listusia: " + i + " " + tab[i]);
        }
        List<String> list = Arrays.asList(tab);
        Collections.reverse(list);
        tab = (String[]) list.toArray();
        for (i = 0; i < tab.length; i++) {
            System.out.println(tab[i]);
        }
        return tab;
    }
}

I would like to return an array, which is printed here:

for(i=0;i<tab.length;i++){
    System.out.println(tab[i]);
}

but I have problem. It's not possible to return tab[i], because it gets me an error "Incompatible types". I need this array to other operations. Could anyone help me?

This is my code after changes:

package iterators;
import java.util.*;
import java.io.*;
public class Nic{
        String abel() throws IOException{
            int i=0;
            BufferedReader wczytaj = new BufferedReader(new FileReader("filmy.txt"));
            StringBuilder sb=new StringBuilder();
            String tekst=wczytaj.readLine();
            while (tekst!=null) {
                sb.append(tekst);
                sb.append("\n");
                tekst=wczytaj.readLine();
            }
            String calosc=sb.toString();
            String film=calosc;
            String znak=",";
            String[]tab=new String[301];
            tab=film.split(znak);
            for(i=0;i<tab.length;i++){ 
                //System.out.println("listusia: "+i+" "+tab[i]);
            }
            List<String> list=Arrays.asList(tab);
            Collections.reverse(list);
            tab = (String[]) list.toArray();
            for(i=0;i<tab.length;i++){
                //System.out.println(tab[i]);
            }
            return tab[i];
        }
    }
Jadea
  • 3
  • 4

2 Answers2

1

List#toArray() returns an Object[] array. use overloaded List#toArray(T[]) instead.

tab = list.toArray(new String[list.size()]);
            for(i=0;i<tab.length;i++){
                System.out.println(tab[i]);
            }
PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

You are getting the error "Incompatible types" because the return type of your abel() method is String[] and you are trying to return a String(tab[i]). If you would like to return tab[i], you must change the return type of your abel() method to String instead of String[].

If you want to return the entire array, use String[] a the return type. It hould work.

import java.util.*;
import java.io.*;


public class Nick {
    String[] abel() throws IOException {
        int i = 0;
        BufferedReader wczytaj = new BufferedReader(new FileReader("filmy.txt"));
        StringBuilder sb = new StringBuilder();
        String tekst = wczytaj.readLine();
        while (tekst != null) {
            sb.append(tekst);
            sb.append("\n");
            tekst = wczytaj.readLine();
        }
        String calosc = sb.toString();
        String film = calosc;
        String znak = ",";
        String[] tab;
        tab = film.split(znak);
        for (i = 0; i < tab.length; i++) {
            System.out.println("listusia: " + i + " " + tab[i]);
        }
        List<String> list = Arrays.asList(tab);
        Collections.reverse(list);
        tab = (String[]) list.toArray();
        for (i = 0; i < tab.length; i++) {
            System.out.println(tab[i]);
        }
        return tab;

    }

    public static void main(String[] args) throws IOException {
        Nick nick = new Nick();
        nick.abel();
    }

}
1218985
  • 7,531
  • 2
  • 25
  • 31
  • Ok, I changed it, but now it gets me "ArrayIndexOutOfBoundsException" :/ – Jadea Mar 02 '13 at 20:06
  • Can you post the full stackTrace? – 1218985 Mar 02 '13 at 20:09
  • java.lang.ArrayIndexOutOfBoundsException: 301 at iterators.Nic.abel(Nic.java:33) at iterators.__SHELL37.run(__SHELL37.java:11) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at bluej.runtime.ExecServer$3.run(ExecServer.java:725) – Jadea Mar 02 '13 at 20:21
  • Could you please post the complete source code a well? – 1218985 Mar 02 '13 at 20:25
  • I added new source code to first post – Jadea Mar 02 '13 at 20:34
  • When you return tab[i], the value at the index position 0 (tab[0]) will be returned. May I know the content of your filmy.txt & what is the value that you are expecting from tab[i]? – 1218985 Mar 02 '13 at 20:42
  • filmy.txt is a file with list of movies. I would like to return array with objects in this list (just like in second loop: for(i=0;i – Jadea Mar 02 '13 at 20:47
  • updated the answer. Please try with that code & let me know whether it i working or not? – 1218985 Mar 02 '13 at 21:02
  • When I try to print tab, it gets me [Ljava.lang.String; – Jadea Mar 02 '13 at 21:15
  • Can you post your complete code or the slice of code where in which you are trying to print the tab. I think, you just tried something like this: System.out.println(tab); without loop. right..? If you want to print the elements in your tab array without any loop, you can try: System.out.println(Arrays.toString(tab)); It will work. – 1218985 Mar 03 '13 at 05:57