0

I need to split a string in every three words (using JAVA). For example:

"This is an example of what I need."

The output would be:

This is an
is an example
an example of
example of what
of what I
what I need
I need

Thanks for the help!

I've tried like this. I don´t know how to get the last two words or what to do when the phrase has less than 3 words.

String phrase = "This is an example of what I need."
String[] splited = phrase.split(" ");

for (int i = 0; i < phraseSplited.length - 2; i++) {
      threeWords = splited[i] + " " + splited[i+1] + " " + splited[i+2];
      System.out.println(threeWords);    
}
MariaH
  • 331
  • 1
  • 8
  • 21

5 Answers5

3

This should work!

First, split all the words into an array of separate words. You can do this, because you know every word is divided by " " (a space).

String[] words = "This is an example of what I need.".split(" ");

Then print like this:

for(int i = 0; i < words.length; i ++) {
    System.out.print(words[i] + " ");

    if(i + 1 < words.length)
        System.out.print(words[i + 1] + " ");

    if(i + 2 < words.length)
        System.out.print(words[i + 2]);

    System.out.print("\n");
}
Tamby Kojak
  • 2,129
  • 1
  • 14
  • 25
  • But this will result in: This is an \n example of what \n I need. That's not the output I'm looking for. – MariaH Mar 31 '14 at 04:46
  • I'm sorry, I think you misunderstood me. I'm saying this solution will result in three lines: Line#1: This is an; Line#2: example of what; Line#3: I need. Please, look at the description of my problem and you will see that is not the correct output. – MariaH Mar 31 '14 at 04:57
  • ahhh, I'm sorry about that. I did misunderstand you. Let me make an edit. – Tamby Kojak Mar 31 '14 at 05:02
  • Now it works fine. Thanks for the help! I just don't need the last single word, but this is a particular case of my problem. And it's also simple to modify your code: `for(int i = 0; i < words.length-1; i ++)` excludes the last word. – MariaH Mar 31 '14 at 05:40
3

Employing almost the same logic as you did , here is the solution :

String myString = "This is an example of what I need.";
        String[] words = myString.split("\\s+");
        for (int i = 0; i < words.length; i++) {
            String threeWords;
              if (i == words.length - 1)
                  threeWords = words[i]; 
              else if(i == words.length - 2)
                  threeWords = words[i] + " " + words[i + 1]; 
              else 
                  threeWords = words[i] + " " + words[i + 1] + " " + words[i + 2];
              System.out.println(threeWords);
    }

Output:

This is an
is an example
an example of
example of what
of what I
what I need.
I need.
need.

But if you require the below output only

This is an
is an example
an example of
example of what
of what I
what I need.
I need.

A slightly modified code would be

for (int i = 0; i < words.length -1; i++) {
            String threeWords;
              if(i == words.length - 2)
                  threeWords = words[i] + " " + words[i + 1]; 
              else 
                  threeWords = words[i] + " " + words[i + 1] + " " + words[i + 2];
              System.out.println(threeWords);
    }
misguided
  • 3,699
  • 21
  • 54
  • 96
2
public static void main(String args[]) throws IOException {
        String phrase = "This is an example of what I need.";
        String splited[] = phrase.split(" ");
        String threeWords;

        for (int i = 0; i < splited.length -1; i++) {
            String a = splited[i];
            String b = splited[i+1];
            String c = null;
            if(i != splited.length-2) {
                c = splited[i+2];
            } else {
                c = "";
            }
            threeWords = a + " " + b + " " + c;
            System.out.println(threeWords);                
        }
    }

output:

This is an
is an example
an example of
example of what
of what I
what I need.
I need.
Min Naing Oo
  • 1,085
  • 5
  • 24
  • 49
0

Try this , this works

Code import java.io.; import java.util.;

public class file1 {

      public static void main(String[] args)
     {

        String word= "This is an example of what I need.";
        String[] wordArray = word.split("\\s+");
        int c;

        for(int i=0;i<wordArray.length; i++)
        {       
            c=i+2;
            for(int j=i;j<=c; j++)
            {
                System.out.println(wordArray[j]);
            }
            System.out.println("\n");
        }
    }
 }
  • I tried this one too, but I got an java.lang.ArrayIndexOutOfBoundsException with the last line. – MariaH Mar 31 '14 at 05:20
  • java.lang.ArrayIndexOutOfBoundsException will occurs , if you are trying to access the array greater than its size, the above code works perfectly for me. Could you paste the code you have tried? –  Mar 31 '14 at 05:38
0

below program will store the combination 3 words in a line so that you can use it

 package com.loknath.lab;

 public class Demo {
public static void main(String[] args) {
    String se = "This is an example of what I need ";
    String temp[] = se.split(" ");
    String temp1[] = new String[temp.length / 3 + 1];

    int line = -1;
    for (int i = 0; i < temp.length; i++) {

        if (i % 3 == 0) {
            line += 1;
            temp1[line]="";
        }

        temp1[line] = temp1[line] + temp[i] + " ";
    }

    for (String stringBuffer : temp1) {
        System.out.println(stringBuffer);
    }

}

}

loknath
  • 1,362
  • 16
  • 25