Once again, I can get part of my code to work, but not all. When the user inputs more than one sentence (ending with either a period, question mark or exclamation point), each sentence should be capitalized accordingly. However, it only works with the first sentence to work. Does anyone have any suggestions on how to correct this? Thx in advance.

import javax.swing.JOptionPane;
public class sentenceStructure
{
	public static void main(String[] args)
	{
	//Get user input
	String s = JOptionPane.showInputDialog(null, "Enter more than one sentence.");
	String[] sarray = s.split("'.','?','!'");

		for (String begin: sarray)
		{

		String capitalize = begin.substring(0, 1).toUpperCase() + 
        				begin.substring(1, begin.length() - 1);
      
		//Show input capitalized
		JOptionPane.showMessageDialog(null, capitalize);
		}

		System.exit(0);
	}
}

Put this line instead of yours

String[] sarray = s.split("[\\.?!]");

Btw '.' is a regular expression which means "match any character"
So use the escape sequence.

The split() function of the String class accepts a Regular Expression as an argument, You seem to be under the impression that it accepts comma separated characters which is incorrect.
If you want to check more about Regular Expressions in Java, this is the best place.
Also note the double back slashes (\\) mentioned by legilimen, the first back slash tell the Java compiler that the following "\" is not an escape sequence ( like "\n" is used to indicate a new line). So in effect when the regex parser encounters it the expression is reduced to "\.?!", this "\" indicates to the regex parser that the following "." is not a regex quantifier.

Well, any other suggestions?

I tried what you said and get the following:

Input: hi. who are you? [Ok]
Message 1 Output: H [Ok]
Message 2 Output: who are yo

To quote the Javadocs this is how substring(int,int) works :-

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex

So this:

begin.substring(1, begin.length() - 1)

Would always skip the last character of your string.

Use this instead :-

begin.substring(1)

Since you want the entire substring till the end.

You need to go through the Java docs of the String class first so that you know how to use these functions. You can check that out here.

Am I on the right track with the following?

String[] strarray = s.split("\\p{Space}");

or, maybe I should be looking at string tokenizer?

Can anyone help?

You have two options:
- If your delimiters are simple single character sequences [. and !] in your case, you can try looping over the entire string and extracting the relevant parts using indexOf and substring
- If your delimiters end up being multi-character sequences you can try to use the hammer of text processing i.e. regular expressions.

A sample implementation:

import java.util.regex.*;

public class SentenceTester {

  public static void main(final String[] args) {
    splitTest();
  }

  // Implement a splitting logic using regex which also works for 
  // multi line strings.
  private static void splitTest() {
    String line = "hello there!!!!! how're you doing? " + 
      "\ni am pretty sure you are doing well.\nright?";

    // Pattern: Any-of-.?!{1, n} followed by whitespace{0, n}
    Pattern pat = Pattern.compile("[.?!]+\\s*", Pattern.DOTALL);

    // Create matcher instance which will match the given regex with
    // the line in consideration.
    Matcher mat = pat.matcher(line);

    int start = 0, end = 0;
    while(mat.find()) {
      start = end;
      // return a `1 based index' into the string where the pattern
      // match ends. Hence when `end' is 17 means character at 
      // position 18 in the string.
      end = mat.end();
      System.out.println("#" + line.substring(start, end) + "#");
    }    
  }

}

> So in effect when the regex parser encounters it the expression is reduced
> to "\.?!", this "\" indicates to the regex parser that the following "." is not a
> regex quantifier.

The quantifiers lose their special meaning when used inside bracketed character classes hence you don't need to escape the DOT; it is treated as a character literal.

Can you help me get started with loop for indexOf and substring?

I've tried so many string options in the last couple of days that my head is spinning...

Thanks in advance.

At least post your attempt in terms of indexOf and substring indicating the problem you are facing.

Didn't see response

String.split() really can help you out, I think. If I understand rightly, the thing you're dissatisfied with is that the suggestion strips off the terminating punctuation from the strings. The expression's a bit more involved, but you can do something such as the following:

String[] sentences = str.split("(?<=[\\.!?]+)(?![\\.!?]+) *");

See the documentation of Pattern to understand how this works. Although the expression's a bit nasty, this really is more succinct than tying yourself in knots with indexOf() and the like.

I leave it as an exercise to the reader to amend the expression to correctly split sentences such as "I asked Dr. Smith to come." :-)

Thank you for your suggestion. Can you direct to me to a good source where I find out how to use this expression? I'd like to finish this and move on to another project involving a JSlider.

Again, thanks for taking the time!

Thank you for your suggestion. Can you direct to me to a good source where I find out how to use this expression? I'd like to finish this and move on to another project involving a JSlider.

Again, thanks for taking the time!

What he has used are called Regular Expressions and in my very first post, if you had cared to see properly, would have found the tutorial for them. But then here it is again:-

http://java.sun.com/docs/books/tutorial/essential/regex/

> String[] sentences = str.split("(?<=[\\.!?]+)(?![\\.!?]+) *");

Like previously mentioned, you don't need to escape regular expression meta characters inside bracketed character classes. Hence [.!?]+ to be used instead of [\\.!?]+ .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.