0

I'm new to Java and used to create arrays and objects in PHP.

Now I wrote a short function in Java for extractions some parts of the text. The function packs up all extracted text pieces into an object and returns to the main code. However whatever I do with the returned result I can't get items back from that.

Here is my code:

public void main(String text) {

    Object[] entities = (Object[])extractNamedEntities(text);

    Object names = entities[0];
    Object locations = entities[1];
    Object orgs = entities[2];

    for (Sting name : names) {
        System.out.println(name);
    }
}


private static Object extractNamedEntities(String text) {

    ArrayList<String> names = new ArrayList<String>();
    ArrayList<String> locations = new ArrayList<String>();
    ArrayList<String> orgs = new ArrayList<String>();

    // Then processing the text and then...

    names.add("Name1");
    names.add("Name2");
    locations.add("Loc1");
    locations.add("Loc2");
    orgs.add("Org1");

    return new Object[]{names, locations, orgs};
}

I get the following error:

Compilation failure for-each not applicable to expression type required: array or java.lang.Iterable found: java.lang.Object

9
  • 1
    names is just a single Object - you cannot iterate it because there is only 1. If you want to handle it as a List you need to cast it to a List. Commented Aug 11, 2021 at 18:57
  • 2
    Why not return a list of lists instead of an object? Commented Aug 11, 2021 at 19:02
  • 1
    You made it a List, but then later you said "forget the list, treat it as an object" when you did this - Object names = entities[0]; You had to do that because your method is returning an Object[] instead of a List of Lists. Basically, why did you do this return new Object[]{names, locations, orgs};? Making Object is throwing away all the useful stuff about the lists you just made. Commented Aug 11, 2021 at 19:03
  • 2
    The type of variable Object names = ... is Object. Because compiler doesn't have any guarantee that value of that variable will always be some kind of iterable object holding strings (since Object type variable can also hold any other type, like Integer, String, Person, etc...) it will not allow us to create code which potentially will try to iterate some non-iterable object since that would break type-safety which is one of main reasons we use Java. Commented Aug 11, 2021 at 19:13
  • 2
    To give compiler such guarantee you can explicitly use casting to ArrayList<String> like ArrayList<String> names = (ArrayList<String>)entities[0]; which will make type of names variable ArrayList<String>. OR don't declare your extractNamedEntities method to return an Object. Since it will always return group of ArrayLists of Strings you can declare that it returns something like List<ArrayList<String>> and return Arrays.asList(names, names, locations, orgs);. Commented Aug 11, 2021 at 19:15

2 Answers 2

1

Java is a statically-typed language. What that means is that you can only call methods that are defined on the (static) type of your object references.

Take this for example:

String s = "hello";
Object o = s;

You have two variables s and o, both holding a reference to a string. You can call s.length() and get the string's length. But you cannot call o.length(), because o is typed as Object and the Object type does not define the length() method.

Define the proper types and you should be fine. Do you see that cast to an Object array when storing the return value of the method? You can get rid of that too if you declare your method to return an Object array directly. Or even better yet, declare it with the actual type that is being returned: List<String>[] (or change the code to return a list instead of an array: List<List<String>>)

Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

the public void main(String text) must be public static void main(String text[]) and the methode signature was also wrong change private static Object extractNamedEntities(String text) to extractNamedEntities(String text[])

import java.util.ArrayList;

public class Test{

    public static void main(String text[]) {

        Object[] entities = (Object[]) extractNamedEntities(text);

        Object names = entities[0];
        Object locations = entities[1];
        Object orgs = entities[2];
        System.out.println(names);
        System.out.println(locations);
        System.out.println(orgs);
    }


    private static Object extractNamedEntities(String text[]) {

        ArrayList < String > names = new ArrayList < String > ();
        ArrayList < String > locations = new ArrayList < String > ();
        ArrayList < String > orgs = new ArrayList < String > ();

        // Then processing the text and then...

        names.add("Name1");
        names.add("Name2");
        locations.add("Loc1");
        locations.add("Loc2");
        orgs.add("Org1");

        return new Object[] {
            names,
            locations,
            orgs
        };
    }
}

1 Comment

No, wait! It is still object and I couldn't iterate through that. But I have managed to keep the structure with ArrayList<ArrayList<String>> and it is working now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.