tjg92
tjg92

Reputation: 197

Splitting a String into an Array that has been Defined

Basically what I need to do is only save the first ten words of a sentence into the Array. I know from my code that all the words will be saved if it is greater than 10, but I only iterate through the loop 10 times so it stops. However, if the sentence has less than 10 words, I need to fill it with an empty string. So I can get it to work if it has 10 or more words. However, I cannot get it to work if there is less than 10 words. Does anyone know of a way to make it work? I have to have an Array size of 10.

String[] tempString = new String[10];
    tempString = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
    for(int i = 0; i < tempString.length; i++)
    {
      System.out.println(tempString[i]);
    }

EDIT:

So essentially if I entered the sentence: "one two three four five six seven eight nine ten," it would work. However, if I entered "one, two, three" it would give me an ArrayOutofBoundsException. What I need the other 7 indexes to be filled with is an empty string.

Upvotes: 2

Views: 192

Answers (5)

giorashc
giorashc

Reputation: 13713

You can use the following :

 for(int i = 0; i < 10; i++)
    {
       if (i < tempString.length)
          System.out.println(tempString[i]);
       else
          System.out.println("");
    }

Since you need 10 prints then you iterate 10 times. Each iteration you check that you are still in the splitted string array bound. If not just fill the rest with an empty space as you desire.

Upvotes: 2

TheLostMind
TheLostMind

Reputation: 36304

There are 2 ways to achieve this..

  1. Initailize all Strings to empty "" (in a for loop..) before you call the for loop shown in your code.

  2. After looping, if length is less than 10, then set remaining items to ""

    if ( tempString.length < 10) ;

    int diff = 10 - tempString.length -1;

Loop diff times and add "" each time..

Sorry about the formatting..

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

String[] tempString = new String[10];
tempString = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");

The two above lines don't make much sense. The first line creates a new array of length 10 and assigns it to the variable tempString, but the second line says: let's forget about this array, and reasign a new array, containing all the words of the sentence, to the tempString variable.

So you actually need two variable:

String[] result = = new String[10];
String allWords = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");

// now copy at most 10 elements from allWords to result:
for (int i = 0; i < result.length && i < allWords.length; i++) {
    result[i] = allWords[i];
}

// and now initialize the rest of the array with empty strings
for (int i = allWords.length; i < result.length; i++) {
    result[i] = "";
}

Upvotes: 1

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

do like this

if(tempString.length<10)
  tempString = Arrays.copyOf(tempString, 10);

Upvotes: 1

mauretto
mauretto

Reputation: 3212

String[] tempString = Arrays.copyOf(sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+"), 10);

Upvotes: 0

Related Questions