| | |
Detecting start of sentence.
Thread Solved
![]() |
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
•
•
•
•
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.
Java Syntax (Toggle Plain Text)
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); } }
•
•
Join Date: Aug 2007
Posts: 9
Reputation:
Solved Threads: 2
Put this line instead of yours
Btw '.' is a regular expression which means "match any character"
So use the escape sequence.
Java Syntax (Toggle Plain Text)
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.
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
To quote the Javadocs this is how substring(int,int) works :-
So this:
Would always skip the last character of your string.
Use this instead :-
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.
•
•
•
•
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
java Syntax (Toggle Plain Text)
begin.substring(1, begin.length() - 1)
Use this instead :-
java Syntax (Toggle Plain Text)
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.
Last edited by stephen84s; Dec 10th, 2008 at 3:36 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
•
•
Join Date: Mar 2008
Posts: 52
Reputation:
Solved Threads: 0
Am I on the right track with the following?
Java Syntax (Toggle Plain Text)
String[] strarray = s.split("\\p{Space}");
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:
> 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.
- 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:
java Syntax (Toggle Plain Text)
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.
Last edited by ~s.o.s~; Dec 10th, 2008 at 1:41 pm.
I don't accept change; I don't deserve to live.
![]() |
Other Threads in the Java Forum
- Previous Thread: JOptionPane tutorial
- Next Thread: How do I concatenate this string
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api applet application array automation binary block bluetooth button character class client code compile component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac map method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql swing system test textfields threads time title tree tutorial-sample ubuntu update windows






