-1

Hello i have a little problem with array conversion

i want to convert this array format ["x","z","y"] To this String Format ('x','y','z') with commas and parentheses.

btw this array is dynamic in size may be increased or decreased

this is my try

public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        String [] arr = {"x","y","z"};
        String s = "(";

        for(int i = 0; i <arr.length;i++){
            s +="'".concat(arr[i]).concat("'") + ',' ;
            if(i == arr.length-1)
                s.replace(',', ')');
        }

        System.out.print(s);

    }

this is my output ('x','y','z', i want to replace the last comma with ')'

7
  • dont expect that the members here will write your code. tell us what is the issue you are facing with the code.. Commented Nov 14, 2016 at 9:48
  • code added check again Commented Nov 14, 2016 at 9:49
  • 1
    Possible duplicate of: stackoverflow.com/questions/5925420/… and an array has a fixed capacity, use an implementation of List if you wish to have dynamic capacity. Commented Nov 14, 2016 at 9:51
  • Use StringBuilder instead of String Commented Nov 14, 2016 at 9:53
  • my string must starts and ends with parentheses my problem with the parentheses in the end of the string. Commented Nov 14, 2016 at 9:53

4 Answers 4

0

This also will do the magic..

Please use StringBuilder for these kind of string operations..

for (int i = 0; i < arr.length; i++) {
            s += "'".concat(arr[i]).concat("'") + ',';
        }

        s = s.concat(")");

        System.out.print(s.replace(",)", ")"));
Sign up to request clarification or add additional context in comments.

2 Comments

('x','y','z',) i don't need the last comma
the last comma will get replaced
0

s.replace(',', ')');

Here you are trying to replace the comma, but Strings in java are immutable, it means you are in fact replacing this, but not assigning the value to any variable, so it's pointless. Also, that replace will not do what you think. Instead, you need to:

s = s.substring(0, s.length()-1);

This is gonna remove the last character from the String (that is extra comma at the end). Then just append ) to it and you are done.

Comments

0

You can also use the String.join method :

public static void main(String[] args) {
    String [] arr = {"x","y","z"};        
    String s  = String.join("','", arr);
    s = "('"+s+"')";
    System.out.print(s);
}

Comments

0

You can try this, using String.format and String.join

    String[] arr = { "x", "y", "z" };
    String s = String.format("('%s')", String.join("','", arr));
    System.out.println(s);

It print

('x','y','z')

Edit

If you are using jdk7, you can use StringBuilder to avoid performance issues, as shown in the code:

    String[] a = { "x", "y", "z" };
    StringBuilder b = new StringBuilder("(");
    for (int i = 0; i < a.length; i++) {
        b.append("'").append(String.valueOf(a[i])).append("'");
        b.append((i == a.length - 1) ? ")" : ", ");
    }
    System.out.println(b);

It print

('x','y','z')

2 Comments

i'm using jdk 7
so you can try the code with the StringBuilder to avoid performance issues if you concatenate using String

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.