user1157751
user1157751

Reputation: 2457

Reading a file in Servlet and Splitting the String

I'm trying to read a file in a servlet, this is done by this code:

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("/AFINN-111.txt");

InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
PrintWriter pw = resp.getWriter();

String text = "";

I'm trying to split a string, for example:

good 2

excellent 3

The strings always has an english word and a numeric value, and I'm trying to store these values in a ArrayList, for example:

ArrayList<String> words = new ArrayList<String>();
ArrayList<Integer> values = new ArrayList<Integer>();

Now, the problem is when I'm trying to split the values:

while ((text = reader.readLine()) != null) 
{
    resp.getWriter().println(text +  "<br />");

    resp.getWriter().println("<br />");
    String[] split_text = text.split(" ");
    //resp.getWriter().println(split_text.length +  "<br />");

    for(int j = 0 ; j < split_text.length; j++)
    {
        resp.getWriter().println(split_text[j] + " <br />");
    }
    resp.getWriter().println("<br />");
    resp.getWriter().println("<br />");

}

String: good 2

Split outcome: good 2

length: 1

When I tried to split the string by space, I get the same string back.

Text file list is found at: http://www2.imm.dtu.dk/pubdb/views/edoc_download.php/6010/zip/imm6010.zip

Solution: Use .split("\t");

Thanks again for everyone's help!

Upvotes: 0

Views: 2459

Answers (3)

sanbhat
sanbhat

Reputation: 17622

Looks like text variable has "good&nbsp2" instead of "good 2"

good&nbsp2 looks like good 2 on browser, cause &nbsp represents single space in HTML

EDIT: You can say text.split("&nbsp;"); to still split them

Upvotes: 1

rozar
rozar

Reputation: 1078

I guess the file has tab space /t instead of the space

Upvotes: 1

Matthias Herlitzius
Matthias Herlitzius

Reputation: 3395

The values in the text file are separated with a tab from the words. This should give you the expected result:

String[] split_text = text.split("\t");

Upvotes: 1

Related Questions