Home > Software engineering >  String.join method not working for array/stack despite me having updated JDK?
String.join method not working for array/stack despite me having updated JDK?

Time:01-19

I'm trying to convert all the character elements in my stack into an element of type String. For example - if my stack has [a,b,c] then I want to use the .join method to create a string "abc".

This is how I'm calling the method:

Stack<Character> mystack = new Stack<>();   
finalstring = String.join("/", mystack);

However, eclipse gives me the following error message:

The method join(CharSequence, CharSequence...) in the type String is not applicable for the arguments (String, Object[])

IntelliJ gives me a similar error message. Searching this message up, the common agreement is that the reason this is happening is that I'm using an earlier version of Java.

However, I am using Java 8. When I write in my command prompt java --version it says I'm using JDK 17. In Eclipse and IntelliJ it also tells me the JDK I'm using is version 17.

I don't know why I'm still getting this error message.

Any ideas would be greatly appreciated.

Here's my full code - I changed the '\' to just quotes:

    public static String backspace(String s) {
        // initial thoughts
        // stack that pushes letters on and then pops when
        // a < character is detected.
        Stack<Character> mystack = new Stack<>();
        String finalstring = "";
        
        if (s.length() == 0) {
            return s;
        }
        
        for (int i = 0; i < s.length(); i  ) {
            
            if (s.charAt(i) == '<') {
                mystack.pop();
                
            } else {
                mystack.push(s.charAt(i));
            }
        }       
        
        finalstring = String.join("", mystack); // error message
        
        return finalstring;
    }

CodePudding user response:

Try it like this. Don't put a slash as the delimiter. Use an empty string. And String.join only works on String types, not Characters. so change your stack to Stack<String>

Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("c");
System.out.println(String.join("", stack));

prints

abc

If you want to join using a Character stack, then do it like this.

String result = stack.stream()
             .map(String::valueOf)
             .collect(Collectors.joining(""));
System.out.println(result);

CodePudding user response:

For example - if my stack has [a,b,c] then I want to use the .join method to create a string "abc".

If you are trying to achieve this, then you will not be using join with the first argument as("/") as this will append a / after appending each element in the String.

import java.util.Stack;

public class StackClass {
  public static void main(String args[]) {
    Stack myStack = new Stack<>(); 
    myStack.add("a");
    myStack.add("b");
    myStack.add("c");
    String finalString = String.join("",myStack);
    System.out.println("final string is ------"  finalString);
  }
}

This is the output

final string is ------abc

I'm using java 16 and I'm sure that java 17 also supports join method on string.

Edit : As seen in your code, you are trying to form a stack of characters. To use the join method, you need the second parameter to be string. Therefore Either form a stack of String or before calling the join method, convert your Character Stack to String stack and then use join method.

  •  Tags:  
  • Related