0

List used to display the set of values get from jtable where as the output results in delimited format of comma separated. For example [data1,data2,data3,data4], this is how the list produces output result. I tried replacing comma into space delimiter of the parameters in arraylist but it replaces all the comma into spaces.

My Code:

List<String> colValues = new ArrayList<String>();
   for (int i = 0; i < jTable1.getRowCount(); i++)
   {
       for(int j=0;j<jTable1.getRowCount();j++)
       {
                colValues.add((String) jTable1.getValueAt(i, j));
        }
    }
            String s1 = colValues.toString();
            JOptionPane.showMessageDialog(null, s1.replace("[", "(").replace("]", ")").replace(","," "));

Output:

Final Output Screenshot

I need to display the records of a row in space delimiter and after the row is printed, the comma delimiter should be used. For example of image screenshot (abc item1,xyz item2), suggest me a solution to do like this example. The scenario is to print output in a single line, suggest me a solution.

Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Arunachalam
  • 51
  • 1
  • 11
  • 1
    Don't start by calling toString() on the list, and then replacing characters in that string. Instead, use a loop and concatenate each element of the list, with the separator you want. Or just use `list.stream().collect(Collectors.joining(" "));`. – JB Nizet Nov 27 '17 at 07:24
  • Where i need to use list.stream().collect(Collectors.joining("")); statement and how to initialize. – Arunachalam Nov 27 '17 at 07:29
  • Where you want to transform your list into a String. That's what your question is all about, isn't it? – JB Nizet Nov 27 '17 at 07:34
  • After transforming list into string. The joining of all list values in a single line and For example of image screenshot (abc item1,xyz item2), after fetching of two values, the comma must involved. Likewise Row_1-column_1 column_2,Row_2-column_1 column_2, space delimiter in between two columns and comma after printing 2 column. – Arunachalam Nov 27 '17 at 08:39
  • list.stream().collect(Collectors.joining("")); is not working, need to add any additional jar files is necessary or not. – Arunachalam Nov 27 '17 at 11:42
  • Define "not working". But it probably means you're not using Java 8, but an obsolete version of Java. – JB Nizet Nov 27 '17 at 12:14

0 Answers0