0

I am having a problem. I have the string src below to be split based on the specified delimiters in an ArrayList. Each section split has to go in an array for further splitting to words. However my problem is that the ! after all is not cater, while the ? is. I encounter that problem if any 2 different delimiters in the list are present in the String src. Can you tell me what is wrong with my code or tell me an easier way to perform the sentence splitting. Thank you for your time.

public class temp
{
    public static void main(String[] args)
    { 
        boolean flag = false;
        int j;
        String word;
        ArrayList <String> delimiter = new ArrayList <String>();
        delimiter.add("!");
        delimiter.add(".");
        delimiter.add("?");
        String src = "Hello all! today is a great day?";
        String parts[] = new String[src.length()];
        String mot[] = new String[src.length()];
        String temp;
        for(int i=0;i<src.length();i++)
        {
            j=0;
            if(src.charAt(i) == delimiter.get(j).charAt(0))
            {
                System.out.println("first");
                parts = src.split("!");
                parts.toString();
        }
            j++;
            if(src.charAt(i) == delimiter.get(j).charAt(0))
            {
                System.out.println("second");
                parts = src.split("\\.");
                parts.toString();
            }
            j++;
            if(src.charAt(i) == delimiter.get(j).charAt(0))
            {
                System.out.println("third");
                parts = src.split("\\?");
                parts.toString();
            }
        }
        System.out.println(Arrays.toString(parts));
        for(int i=0;i<parts.length;i++)
        {
            word = parts[i];
            mot = word.toLowerCase().split(" ");
        }
        System.out.println(Arrays.toString(mot));
    }
}

my output is as follows : first

third [Hello all! today is a great day] [hello, all!, today, is, a, great, day]

Aimee Borda
  • 842
  • 2
  • 11
  • 22
daam
  • 23
  • 5
  • What is the purpose of `parts.toString();`? You are doing nothing with the result of that method call. – Sentry Mar 14 '17 at 07:01

2 Answers2

1

Obviously the "!" after "all" will no get split as because after taking "!" out you are storing the delimited string in "parts" but later on you are carrying out delimiting operation on "src" which contains the original string including the "!"

Ashwani Kumar Rahul
  • 776
  • 1
  • 6
  • 14
  • please check your code flow as it seems you need to modify your program because the your code is not executing as per the requirement mentioned by you. For reference about delimiting operation please check : - http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java?rq=1 – Ashwani Kumar Rahul Mar 14 '17 at 07:07
  • yes i see that, it is not working as i want thanks for your time and effort. Thank you – daam Mar 14 '17 at 07:30
  • @zfk16 please mark an answer if it answered your question – moffeltje Mar 14 '17 at 10:36
0

UPDATE

I figured it out it is very easy... Simply create a String of regex such as:

    String regex = "[!.?]";
    String src = "Hello all! today is a great day?";
    String mot[] = src.split(regex);
    System.out.println(Arrays.toString(mot));

Output: [Hello all, today is a great day]

daam
  • 23
  • 5