7
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').

7 Answers 7

12

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); } } } 
Sign up to request clarification or add additional context in comments.

Comments

2

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(); 

Comments

1

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)

Comments

1

In Android/Java this is very simple. Follow this :-

Suppose

String txt="I am Rahul Kushwaha.I am an Android Developer"; 

Now, To split this into line by line.use split() method .Like:-

String txt_split=txt.split("\n"); System.out.println(txt_split); 

OutPut :-

I am Rahul Kushwaha . I am an Android Developer 

Now, To get Length of Splited String use lenght() method.

int txt_length=txt_split.length(); System.out.println("text length=",txt_length); 

Output:-

text length=10 

To particular data by line .Do like this :-

String[] text_data=txt.split("\n"); System.out.println("Name :",text_data[2]); System.out.println("Title :",text_data[3]); System.out.println("Job :",text_data[8]); 

OutPut:-

Name :Rahul Title :Kushwaha Job :Android 

Hope this will help you.Thanks...

Comments

0

use Arraylist for this..

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

=====>Checking

 for(int i=0;i<items.size;i++) { System.out.println("item name "+items.get(i)); } 

Comments

0

How to split string by newline

I came to this question just wanting to split a string, not a file. So I will answer that here for future visitors.

Given

String myString = "one/ntwo/nthree"; 

You can convert it into an array with

String myArray[] = myString.split("\n"); // {"one", "two", "three"}; 

And if needed, that can be converted into a List with

List myList = new ArrayList(); Collections.addAll(myList, myArray); 

Comments

0

if you want to keep blank lines if at all then you can use

String lines[] = givenString.split("\\r?\\n", -1); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.