0

I have a Text file (.txt) that contains data strings using comma separated values, i.e

jordan,hello,12th Feb 15:23, pending

I would like to then pull this data into a HTML table with the ",' separating each column. For example the headings of the table would be:

Name   Question   Date    Status

Thus Jordan would go in the name column and hello under question etc.

Currently I have output the full string but I need the individual elements.

Any advice would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Brug9
  • 1
  • 1
  • 1

3 Answers3

1

You need a parser to read csv file and create individual elements. You can either use String.split(...) or even better leverage CSV parsing libraries. Create a class Data and populate it with the parsed data (each row has a corresponding Data object). You should have a List<Data> after the entire file has been parsed which you can pass to the JSP page. JSP then iterates through the List and creates the table.

Community
  • 1
  • 1
Aravind Yarram
  • 76,625
  • 45
  • 224
  • 313
0

Assuming that you don't have a , in the data, simply call split(",") on each line and create a custom formatted HTML table, something like this (haven't tested):

out.println("<table>")

for (int i=0; i<lines.length; ++i) {
    out.println("<tr>" )
    String[] data = line[i].split(",");

    for (String val : data) {
        out.println("<td>" + val + "</td>")
    }

    out.println("</tr>" )
}

out.println("</table>")
rlegendi
  • 10,202
  • 3
  • 37
  • 49
0

You can use the String#Split method to convert your actual String to an array of Strings with all the values:

String s = "jordan,hello,12th Feb 15:23, pending"
String[] sArray = s.split(",");
for(String si : sArray) {
    //you can print them in your output
}
Luiggi Mendoza
  • 83,472
  • 15
  • 149
  • 315