-
using the StringBuffer class
I'm trying to create a method the receives a String from a dialog box in lower case and takes that string and makes every first word of the sentence in that string upper case...i.e. "go red sox. beat new york. reverse the curse." and then would return the string
GO red sox. BEAT new york. REVERSE the curse.
With these coordinates I can achieve the output.
StringBuffer strBuf = new StringBuffer(s);
String str=s;
strBuf.replace(0,str.length(),str.toUpperCase());
strBuf.replace(2,10,str.substring(2,10).toLowerCase()); strBuf.replace(16,25,str.substring(16,25).toLowerCase());
strBuf.replace(34,44,str.substring(34,44).toLowerCase());
I was trying this: to capture the periods and the array does give me 10, 25,44
for (i=0; i < s.length(); i++){
if (s.charAt(i)== '.'){
hold2= a12[x]=i;
}
But I'm having a problem capturing the spaces
if (s.charAt(i)== ' '){
hold= a1[y]=i;
gives all the spaces but I only need 2,16,34..
and lastly, how can I process the array to meet the above coordinates?
Thanks much..
-
not sure on the code but couldnt you grab the String as a whole, then divide it up into different sentances based on the full stop.
Then place those 3 divisions into an array, then for each element in the array you could get the first bit of text up to the space and the convert what ou have just selected with the toUpperCase() method of the String class
??
I hope that helps a little
A kram a day keeps the doctor......guessing
-
using the StringBuffer class
That sounds logical, though I have no idea how to set up and process a String array seperated by the period? I'm open for suggestions...lol. Is it possible to use the StringTokenizer? And if so how?
Thanks...
-
there is a method to split a string into an array based on a delimeter (in this case the fullstop), i think its simply
split(string s, char d)
where s is the sentance and d is the fullstop.
This will return an array with your 3 smaller sentances in it.
go here and look it up:
http://java.sun.com/j2se/1.3/docs/api/
or more accuratly:
http://java.sun.com/j2se/1.4.2/docs/...Tokenizer.html
look at the example that uses the split() method
A kram a day keeps the doctor......guessing
-
using the StringBuffer class
Ok cool, so far that worked but, I'm fuzzy on how to process the uppercase on the first word of each sentence. It seems like I'm having problems conditioning uppercase till the first space, then lowercase, using the arrays? Then after that how do I append the array back to a string?
--so far....
import java.util.StringTokenizer;
public class AnotherTest
{
public static void main(String[] args)
{
String s="go red sox. beat new york. reverse the curse.";
StringTokenizer st = new StringTokenizer(s);
String[] result = s.split("\\.");
for (int x=0; x<result.length; x++)
System.out.println(result[x].toUpperCase());
}
}
Thanks much for the help!
-
here is some quick code i whipped up just now, i havent checked it but it should be ok:
Code:
import java.util.StringTokenizer;
public class AnotherTest
{
public static void main(String[] args)
{
String s="go red sox. beat new york. reverse the curse.";
StringTokenizer st = new StringTokenizer(s);
String[] result = s.split("\\.");
String[] finalResult = new String[result.length];
for (int x=0; x<result.length; x++){
result[x] = result[x].trim();
int spaceIndex = result[x].indexOf(" ");
String tempString = result[x].substring(0,spaceIndex);
tempString = tempString.toUpperCase();
finalResult[x] = tempString + result[x].substring(spaceIndex,result[x].length());
System.out.println(finalResult[x]);
}
}
}
whoops missed a bit, now its all good
A kram a day keeps the doctor......guessing
-
using the StringBuffer class
Awesome!! And a nice map to study from. Is it possible to get the finalResults[x]; back to a sentence
GO red sox. BEAT new york. REVERSE the curse.
for display?
Thanks once again....
-
try it,
create a new array, then just go through the finalResult array and each element in the finalResult array (i.e. each smaller sentance) add one at a time to the new array
A kram a day keeps the doctor......guessing
-
using the StringBuffer class
Cool that worked very well...thanks so much for the knowledge!
-
not a problem, you know how to read the java API? very handy!!
http://java.sun.com/j2se/1.3/docs/api/
also GOOGLE!!
let us know how you go on those other questions
A kram a day keeps the doctor......guessing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks