To finds the longest word (characters separated by delimiter characters) in the String s, using blanks, commas, and tabs as delimiters.
// Assume s contains a string of words
String longestWord = "";
StringTokenizer st = new StringTokenizer(s, " ,\t");
while (st.hasMoreTokens()) {
String w = st.nextToken();
if (w.length() > longestWord.length()) {
longestWord = w;
}
}
World of tricky Core Java Q and A Covering Object Oriented Analysis and Design, JVM Internals,Java Language Fundamentals,Datatypes,Keywords,Operators and Assignments,Identifies,Declarations and Modifiers, Conversion,Casting and Promotion, Flow control,Assertions, Exception Handling and Garbage Collection,Objects and Classes), Basic Packages and their classes,JDBC,JFC Swing,Java Server Pages (JSP), Servlets,EJB,JMS,JNDI etc and Open source technologies like Struts,Hibernate,Spring etc
Search This Blog
Friday, April 21, 2006
Finding the longest word in a String using StringTokenizer : Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment