3

this may be pretty simple, but for some reason I am blanking right now.

Suppose I had a string "Hello I Like Sports"

How would I add each word to an arraylist (so each word is in an index in the arraylist)?

Thanks in advance!

5
  • 3
    Did you attempt anything yourself? Commented May 24, 2013 at 2:48
  • 2
    Better for you to think of the steps necessary for you to do this. The steps can be in English, not Java if you don't know the code, but consider posting your algorithm here. This type of exercise can help improve your code thinking if done regularly. It also would help you gain some creds here, showing that you've at least tried something and would make your post less of a "homework dump" type question. Commented May 24, 2013 at 2:48
  • 3
    foreach (String s : string.split(\\s+)) { myArrayList.add(s); } Commented May 24, 2013 at 2:49
  • Yea basically I tried looping through the string, and for every character that came before a space I would add it to a string. And then at the end I would add that string to an arraylist of strings. What I realized though was that only got me the first word, not anything after. Commented May 24, 2013 at 2:51
  • 4
    @Big_Fan: always better to post your attempt. We greatly appreciate just the mere fact that you've tried and are willing to show us the attempt. Commented May 24, 2013 at 2:52

8 Answers 8

10
ArrayList<String> wordArrayList = new ArrayList<String>();
for(String word : "Hello I like Sports".split(" ")) {
    wordArrayList.add(word);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Eric: Sorry, I fixed that
5

The first thing to do is to split that sentence up into pieces. The way to do that is to use String.split That will return an Array of Strings.

Since you want it in an ArrayList, the next thing you have to do is loop through every String in the array and add it to an ArrayList

1 Comment

Thanks! Never knew about string.split, this will help a lot!
5
String[] words = sentence.split(" ");  
list.addAll(Arrays.asList(words));

Comments

2

Try this:

public static void main(String[] args) {
        String stri="Hello I Like Sports";
        String strar[]=stri.split(" ");
        ArrayList<String> arr = new ArrayList<String>(Arrays.asList(strar));
        for(int x=0;x<arr.size();x++){
            System.out.println("Data :"+arr.get(x));
        }
    }

Output :

Data :Hello
Data :I
Data :Like
Data :Sports

Comments

1

you can use the split method of the String and split on spaces to get each word in a String array. You can then use that array to create an arrayList

String sentence ="Hello I Like Sports";
String [] words = sentence.split(" ");
ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(words));

String.split()

Arrays.asList()

Comments

0

A little search would have done the work.

Still I am giving a solution to this. You can use Split.

You can later add those array elements to arraylist if you require.

    String s="Hello  I like Sports";
    String[] words = s.split(" "); //getting into array
    //adding array elements to arraylist using enhanced for loop
    List<String> wordList=new ArrayList();
    for(String str:words)
    {
     wordList.add(str);
    }

Comments

0

First, you have to split the string, and :

Sample code :

final String str = "Hello I Like Sports";
// Create a List
final List<String> list = Arrays.asList(str.split(" "));
// Create an ArrayList
final ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(str.split(" ")));

Using Arrays.asList and the ArrayList constructor avoids you to iterate on each element of the list manually.

1 Comment

eh you generally declare based on the interface. -1 for bad form.
0

try this code was perfect work for get all word from .txt file

reader = new BufferedReader(
    new InputStreamReader(getAssets().open("inputNews.txt")));

    // do reading, usually loop until end of file reading
    String mLine;
    while ((mLine = reader.readLine()) != null) {
        for(String word :mLine.split(" ")) {
            lst.add(word);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.