The method join (String, List<String>) is undefined for the type String

Viewed 5000

I'm learning Java and I have come across one tutorial about Hash Maps and start typing the same code like my tutor. The problem is when I came across String.join method, I got an error and my tutor didn't, and I do not know why.

This is the error:

The method join(String, List<String>) is undefined for the 
type String

Can you tell me whats the problem?

private String getZnanjaList(){

    List <String> listaZnanja = new ArrayList<>();

    for (String znanje : this.znanja.keySet()){
        listaZnanja.add(znanje + ": " + this.znanja.get(znanje));
    }

    return String.join("; ", listaZnanja);
}
3 Answers

When you turn to the javadoc for String.join() you find:

public static String join(CharSequence delimiter,
                          Iterable<? extends CharSequence> elements)
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
...
Since:
1.8

That is all there is to this: you must be using an older version of Java.

So: you should check what JDK version is available on your system (respectively used when you invoke javac, or what your IDE points to), and either restrict yourself to things that work with that version, or (recommended) directly hop to Java 8, or Java 11.

As others mentioned, that String feature appears in Java 8 and onwards.

If you are intended to stick to Java 7, you can use the following solution:

private String getZnanjaList(){
    List <String> listaZnanja = new ArrayList<>();
    /*Map<String, String> znanja = new HashMap<>();
    znanja.put("fruit", "apple");
    znanja.put("animal", "dog");
    znanja.put("vehicle", "ford");*/

    for (String znanje : znanja.keySet()){
        listaZnanja.add(znanje + ": " + znanja.get(znanje));
    }

    StringBuilder sb = new StringBuilder();
    for(String s : listaZnanja) {
      sb.append(";").append(s);
    }
    return sb.deleteCharAt(0).toString();
}

In this approach, we take an advantage of the StringBuilder class.
Further information you can find in doc

The error message is telling you that the type String does not have a method with signature join(String, List<String>). Since String in Java 8 does have the method String.join(CharSequence, Iterable<CharSequence>) which would match (String is a type of CharSequence and List is a type of Iterable), this means that you aren't using Java 8 or newer.

Possible solutions:

  1. Update your JRE or JDK to Java 8 or later.
  2. Change things such that your code is using a Java 8 or later JRE or JDK that is already on your machine.
  3. Alter the code so that it doesn't rely on Java 8 features.

Updating to Java 8 or later is the obvious choice. Just be careful that you are using that JRE/JDK and not a different one. For example, Microsoft often sets the path to point to its JRE, which may be older.

From a command prompt, you can run

java -version

to check your version.

There are already a number of sites on the internet that can help you with pointing your Java at the correct JDK or JRE. E.g. this answer. If you are still experiencing trouble with that, you may want to post a new question with your %PATH% and %JAVA_HOME% variables (I'm assuming Windows) as well as the location of the JRE/JDK that you want to use.

If you just need to update, searching for "download Java 8" should get you there.

This answer explains how one might do this manually. Other answers on the same question suggest helpful libraries. But really, updating to use Java 8 is probably better.

Related