0

I tried splitting up the words in the line and checking if they had periods, but I got an error:

falls.falls.falls.Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at Alpha.main(Alpha.java:10)     

Code:

import java.io.*;
import java.util.*;

public class Alpha
{
    public static void main(String[] args)
    {
        String phrase = "the moon falls. the flowers grew.";
        String beta = "";
        String[] array = phrase.split(" ");
        for (int i = 0; i < array.length; i++)
        {
            if (array[i].endsWith("."))
            {
                array[i + 1] = array[i + 1].substring(0, 1).toUpperCase() + array[i + 1].substring(1);

                beta = beta + array[i];
            }
            System.out.print(beta);
        }
    }
}

(Also I don't think that's how I would call another word of an array, any suggestions on how I can fix that?)

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • 3
    Imagine what will happen in your code when it reaches the terminal period to see why you're getting that exception (hint `array[i + 1]` will do what when this happens?). Why not simply iterate through the String one character at a time? – Hovercraft Full Of Eels Nov 18 '12 at 04:08

4 Answers4

1

You are not handling the case where your input ends with .. Also, general sentences may have a space after . before the next sentence follows. You should consider that too. Also, you may want to look at this version of indexOf that takes a fromIndex.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
0

I would suggest using split() on ".". That way, you could then check if there were a character following the period, and then capitalize it.

awolfe91
  • 1,627
  • 1
  • 11
  • 11
0

Based on the code of the question Regular expression match a sentence

import java.util.regex.*;
public class TEST {
    public static void main(String[] args) {
        String subjectString = 
        "This is a sentence. " +
        "So is \"this\"! And is \"this?\" " +
        "This is 'stackoverflow.com!' " +
        "Hello World";
        String[] sentences = null;
        Pattern re = Pattern.compile(
            "# Match a sentence ending in punctuation or EOS.\n" +
            "[^.!?\\s]    # First char is non-punct, non-ws\n" +
            "[^.!?]*      # Greedily consume up to punctuation.\n" +
            "(?:          # Group for unrolling the loop.\n" +
            "  [.!?]      # (special) inner punctuation ok if\n" +
            "  (?!['\"]?\\s|$)  # not followed by ws or EOS.\n" +
            "  [^.!?]*    # Greedily consume up to punctuation.\n" +
            ")*           # Zero or more (special normal*)\n" +
            "[.!?]?       # Optional ending punctuation.\n" +
            "['\"]?       # Optional closing quote.\n" +
            "(?=\\s|$)", 
            Pattern.MULTILINE | Pattern.COMMENTS);
        Matcher reMatcher = re.matcher(subjectString);
        while (reMatcher.find()) {
            String sentence = reMatcher.group();
            sentence = sentence.substring(0,1).toUpperCase() + sentence.substring(1);
            System.out.println(sentence);
        } 
    }
}
Community
  • 1
  • 1
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
0

Consider this:

    String phrase = "the moon falls. the flowers grew.";
    char[] a = phrase.toCharArray();
    boolean f = true;
    for (int i = 0; i < a.length; i++) {
        if (f) {
            if (a[i] != ' ') {
                a[i] = Character.toUpperCase(a[i]);
                f = false;
            }
        } else if (a[i] == '.') {
            f = true;
        }
    }
    phrase = new String(a);
    System.out.println(phrase);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275