The Artima Developer Community
Sponsored Link

Java Answers Forum
Some hints would be awesooome

5 replies on 1 page. Most recent reply: May 22, 2004 8:10 PM by Daniel Ray

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Jono

Posts: 3
Nickname: jonoha
Registered: May, 2004

Some hints would be awesooome Posted: May 19, 2004 5:36 PM
Reply to this message Reply
Advertisement
heya guys.
just a question bout a question i got. using
stringtokenizer we are required to create a sentence or phrase checker that has to correct certain errors.

The program will not do the job perfectly, but it will enforce the following rules:

Sentences start with a letter in upper case.
*There should be exactly one space between each word, except when there is a period.
*All other letters must be in lower case.
*There should be two spaces after a period (.).
*There should be no spaces before a period.
*There should be one space after a comma (,).
*There should be no spaces before a comma.


To me my understanding is to use StringTokenizer to split every word up. first change every letter to lowercase, and then do the checks in the order of above? and fix them one by one? use a Gui interface? is that sort of the right way about all this, i been lookin at it for a while and think it is but when comes to coding just a bit sorta out there gettin there very slowly, but yeah any help would be awesome. cheers fellas


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Some hints would be awesooome Posted: May 21, 2004 2:18 AM
Reply to this message Reply
Okay, here's how I did it at 4am my time. I'm posting this code because one, it is brute force and two, it has a bug at the end that you should be able to solve. I didn't double check my testcases either, so you may find other bugs. The point is, my testcases showed me why the extra comma is appearing at the end. You could also play around with the sequence of the method calls to get some funky results and it will help you understand a little more about the String library. If you can't figure out how to remove the bug .. let me know.

public class StringManipulator {
private String text;

public StringManipulator() {
text = "Please, write your test cases first .Please , write your test cases first.";

lowerCaseAllText();
ensureOnlyOneSpaceBetweenWords();
ensureTwoSpaceAfterPeriod();
ensureNoSpacesBeforeCharacter(".");
ensureNoSpacesBeforeCharacter(",");
upperCaseFirstLetterOfSentences();

}

private void lowerCaseAllText(){
text = text.toLowerCase();
System.out.println(text);
}

private void ensureOnlyOneSpaceBetweenWords(){
StringBuffer buffer = new StringBuffer();
StringTokenizer tokenizer = new StringTokenizer(text, " ");

while(tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken();
buffer.append(token);
buffer.append(" ");
}
text = buffer.toString();
System.out.println(text);
}

private void ensureTwoSpaceAfterPeriod(){
StringBuffer buffer = new StringBuffer();
StringTokenizer tokenizer = new StringTokenizer(text, ".", true);

while(tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken();
buffer.append(token);
if(token.equals(".")){
buffer.append(" ");
}
}
text = buffer.toString();
System.out.println(text);
}

private void ensureNoSpacesBeforeCharacter(String character){
StringBuffer buffer = new StringBuffer(text);
String badFormat = " " + character;
int index = buffer.indexOf(badFormat);

while(index != -1){
buffer.delete(index, index + 1);
index = buffer.indexOf(badFormat);
}
text = buffer.toString();
System.out.println(text);
}

private void upperCaseFirstLetterOfSentences(){
StringBuffer buffer = new StringBuffer();
StringTokenizer tokenizer = new StringTokenizer(text, ".");

while(tokenizer.hasMoreTokens()){
String token = tokenizer.nextToken();
token = upperCaseFirstLetterOfSentence(token);
buffer.append(token)
.append(". ");
}
text = buffer.toString();
System.out.println(text);
}

private String upperCaseFirstLetterOfSentence(String sentence){
sentence = sentence.trim();
int length = sentence.length();

if(length > 0){
String firstChar = text.substring(0, 1);
String theRest = text.substring(1, length);

sentence = firstChar.toUpperCase() + theRest;
}
return sentence;
}

public static void main(String[] args) {
new StringManipulator();
}

}

Jono

Posts: 3
Nickname: jonoha
Registered: May, 2004

Re: Some hints would be awesooome Posted: May 21, 2004 6:41 AM
Reply to this message Reply
yeah sweet as. i implemented terminalIO into the program so that it will prompt the user for a paragraph and then edit that paragraph. however i am looking through the code atm trying to find the error, but when a '.' is input into the paragraph then it throws the result out. Like if i input "hello this is jono. How are you?" i get "Hello this is jono. Hello this ." in return. it repeats after a period. and i know it is in the last part in the method upperCaseFirstLetterOfSentence method. becuase as it goes through it works fine and everything else changes fine, just when it gets to creating the capitals it chokes. Cheers for ya help. hopeuflly i can get this one goin

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Some hints would be awesooome Posted: May 21, 2004 12:09 PM
Reply to this message Reply
My original comment was wrong, but I'm glad you understood that I meant a 'period' instead of a comma. Hard to think at 4am. Here's some direction to why the upper... method is broken. The while loop is iterated 3 times even though there are only 2 periods in the test string and this causes the extra period.

Jono

Posts: 3
Nickname: jonoha
Registered: May, 2004

Re: Some hints would be awesooome Posted: May 22, 2004 3:46 AM
Reply to this message Reply
i have sussed out exactly what it is doing wrong with the repeating thing but just cant see it in the code yet. if i type
"Hello i hope this works. wonder"
it will return
"Hello I hope this works. Hello ."

Where the last hello in the returned sentece is the same about of chars as wonder in the first. So in the return when there is a period it will start the first sentence again until the amount of chars is reached.
ie
"explainin this stuff. Hard"
it will return
"Explainin this stuff. Expl"
where expl has the same amount of chars as Hard. just printed off the code so i dont hurt me eyes glaring at this screen all nite, and eha gonna have a look through it again now. cheers for ur help thru this aye, muchly appreciated!!
jono

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Some hints would be awesooome Posted: May 22, 2004 8:10 PM
Reply to this message Reply
You uncovered a flaw in my testcase. I assumed that every sentence would end in a period. The problem now is to rewrite upperCaseFirstLetterOfSentences. The tokenizer only finds one token because it considers a token as anything up to a period.

I think I would start by removing the tokenzier and use String methods like indexOf and substring to fix this.

Flat View: This topic has 5 replies on 1 page
Topic: dinamyc cast to an object Previous Topic   Next Topic Topic: graphics

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use