Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split string by 'newline'?

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str
while ((str =in.readLine()) != null)
{
     items = str.split("\n");
}
in.close();

String (str) contains data from a text file like:

January

February

March

etc.

Each word is on ag new line. I want to read the string and separate each word on a new line and store into an array of String objects (this would be the variable named 'items').

like image 419
amg Avatar asked Aug 14 '12 05:08

amg


3 Answers

Actually, BufferedReader.readLine already splits the input based on newlines.

So, where you currently have:

items=str.split("\n");

you only need to append str to your array.

For example, with the infile file holding:

January
February
March
April
May
June

the following program outputs 6 (the size of the array list created):

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
class Test {
    public static void main (String[] args) {
        try {
            ArrayList<String> itms = new ArrayList<String> ();
            BufferedReader br = new BufferedReader (new FileReader ("infile"));
            String str;
            while ((str = br.readLine()) != null)
                itms.add(str);
            br.close();
            System.out.println (itms.size());
        } catch (Exception e) {
            System.out.println ("Exception: " + e);
        }
    }
}
like image 97
paxdiablo Avatar answered Nov 26 '22 17:11

paxdiablo


The readLine method already reads line-by-line. There will be no \n characters in this string.

Try this instead:

ArrayList<String> itemList = new ArrayList<String>();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
    itemList.add(str);
}
in.close();
like image 44
Cat Avatar answered Nov 26 '22 19:11

Cat


this my code and its work for me

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            ArrayList<String> itemList = new ArrayList<String>();
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content, "iso-8859-1"));
                StringBuilder sb = new StringBuilder();

                String s = null;

                while ((s = buffer.readLine()) != null) {

                    itemList.add(s);


                }

                response = itemList.get(0);
                content.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        textView.setText(Html.fromHtml(result));
    }
}

public void readWebpage(View view) {
    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute(new String[] { "http://www.yourURL.com" });

}

readWebpage function is onclick function I create in my button xml like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/readWebpage" android:onClick="readWebpage" android:text="Load Webpage"></Button>
<TextView android:id="@+id/TextView01" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Example Text"></TextView>

in my code I try to get the first line so I use itemList.get(0) if you want to get another line just change the index like itemList.get(1) or itemList.get(n)

like image 24
Firhat Avatar answered Nov 26 '22 19:11

Firhat