aspose file tools*
The moose likes Beginning Java and the fly likes Changing spelling 'or' to 'our' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing spelling Watch "Changing spelling New topic
Author

Changing spelling 'or' to 'our'

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Hi all, I need how to read a line of text and change the spelling of the words ending in "or" to end in "our". I have to do this for a project at school. Please help me with this.

BYE

[ EJFH: Edited subject, removed invitation to take it offline. ]
[ April 22, 2005: Message edited by: Ernest Friedman-Hill ]

Note: I love programming.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
What have you tried so far, and where are you stuck? We try to help people learn Java here instead of doing their homework which would hinder learning.

Do you know how to read a single line or is it passed to your program? I see from your other post that you know how to look at charactes inside a String; are you familiar with the other String methods? Are you familiar with the StringTokenizer class?
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
What makes this seemingly-trivial problem urgent?


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Originally posted by Jeff Bosch:
What makes this seemingly-trivial problem urgent?


Don't you know that putting 'URGENT' in your topic forces people to write code and solve problems for you? Especially when it's in all caps.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
As Jeff and Steven are saying in their own way is that you might want to read How to Ask Questions the Smart Way. You might want to concentrate on "Ease Up" and "Use A Meaningful Subject Line". Also, the first "Other Resource" might provide some insight.

One other thing that isn't made really clear in that document is that we are here to help you learn, not to do your homework for you. If you want to know why your program is running out of memory, or why a certain line won't compile, we'd be happy to help. Even if you have the beginnings of an idea on how to start with your assignment and want some feedback, go ahead and ask. However, don't just plop your assignment that (as far as we can tell) you haven't even thought about here and expect us to lead you through the whole solution. If I interpret his post correctly, this is what David was getting at.

I hope this helps. Really... I do.

Ryan
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Jeff Bosch:
What makes this seemingly-trivial problem urgent?


LONDON (Reuters) - The British Security Service, in a rare press conference, has announced that they are finally calling off the search for Agent 0x007, whose real name can now be unveiled as being Manuel Diaz.

"When last we heard from agent Diaz," Director-General Eliza Manningham-Buller said, "he was deep undercover, infiltrating a terrorist network in the Middle East. That was three weeks ago."

Diaz's mission involved the strategic use of computer documents that were supposedly from Canada, but were actually forgeries from the United States.

"Unfortunately, we found out that there were many, many words ending in 'or', instead of the preferred Canadian and British ending of 'our'," Manningham-Buller continued, "Our only hope now is that Diaz was somehow able to find a way... any way... to quickly convert those documents before his cover was blown."

This is the first such mishap in MI5 history since the notorious "Hello, World!" fiasco that claimed the lives of double-agents Brian Kernighan and Dennis Ritchie.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Holy cow, I'm at work and I almost busted out laughing. I could hardly hold it in. Never do that again!!!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Or there could be dor <corrected to dour by some unknown agent> implications!
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Originally posted by Jeff Bosch:
Or there could be dor <corrected to dour by some unknown agent> implications!


I dont know what did I to deserve all this kind of "messages". I was not expecting this. Im sorry if I offend you in some way, Im really not good in Java, i started to study this since i entered in school. My programming language is Object Pascal, so.. I dont know anything about java programming. I post this in here because i really need help with this this code. I tried to do my best, but i have no idea have this to work with this type of strings. I only know so far, how to read the line, work with the tokens.... but... I only know how to change the spelling of string working with a character. Please again. Help me with this code.

BYE SORRy
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
The first four messages are the real ones. Reading them will help you get more use of and assistance from JavaRanch.

Consider writing off the remainders as a late Friday's rantings. You see, we get so many people posting problems without having read the FAQ about how to post an on-point question, that sometimes we get goofy (and, occasionally, testy) about vague posts that really give very little information to go on yet are very often marked URGENT. And we also get a lot of people who simply post their homework assignment, without having taken a shot at it first, then ask for urgent help because the assignment's due tomorrow and the poster hasn't done a damned thing with it.

The best way to get help here is to try tackling whatever problem you have, posting relevant code snippets (using UBB Code tags, of course!), showing what the error was or any compiler problems, then asking specific questions. I think you'll see a whole different breed of response if you do that.

One specific tip: Read the Java Tutorial on regular expressions.
[ April 22, 2005: Message edited by: Jeff Bosch ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Manuel,

You didn't offend anybody, and nobody here meant to offend you, but you didn't give us anything to work with.

You say you can read the line in and work with tokens, so what are you having problems with?

Post the code you have so far, be sure to use code tags. Tell us specifically where you are stuck, or what is not working. We will be glad to help, but we are not going to just write code and post it for you.

Read what is in Ryan McGuire's post and the information he linked to regading asking questions the smart way.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Manuel Diaz:


I dont know what did I to deserve all this kind of "messages"....


It's actually more what you DIDN'T do. You didn't give us enough information to really help you. We often get posts here that, no matter how disguised, boil down to "Will you do my homework for me?" While you probably didn't mean it this way, many of the participants on the board read it as such.

So in order to get the help you want, you should check out the link that Ryan McGuire gave in his post above. It will give some suggestions about how to ask a question in such a way that people are willing to help. One important thing is to illustrate what you have done on your own. You can do this by posting code, compiler errors, runtime errors, etc.

Enough of my soap box. Please follow the link that Ryan gave and come back with some specific questions. You'll find that we are much more friendly when you make some semblance of effort in posing your questions.

Layne
[ April 22, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
See the common thread among our responses?
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
I have no compile errors. But, the code does not do what i want it to do. I dont know any method to change strings. I know this one but is only for characters I thinks is:

string = (string1.replace('i','y'));

But when a tried to do this:

string = (string1.replace('or','our'));

That give a me a error. Like there is not such method. So, i dont know another way to do this. I only know that I have to separate the line in tokens, to read each word independiently.

BYE AGAIN.
[ April 22, 2005: Message edited by: Manuel Diaz ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805


You have single quotes surrounding strings. It won't like that.

Have you checked out regular expressions? I think you'll find what you're looking for there...
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Originally posted by Jeff Bosch:


You have single quotes surrounding strings. It won't like that.

Have you checked out regular expressions? I think you'll find what you're looking for there...


That does not work. Ok im going to post here a part of my code, and u can see it.


String word,string1;
c.println("Enter a line");
word.readLine();
string1 = word.replace("or","our");
c.println(string1);
------ When I do this, it does not compile, wrong!.-------
string1 = word.replace("or","our");
-------- And if I change it to this, wrong too------------
string1 = word.replace('or','our');
-------- BUT if I do this is FINE!------------------------
string1 = word.replace('o','r');

Because this method is for change the spelling only working with characters, it does not work for strings!.

BYE
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805


Word is a String. String has no such method. Interesting that it would compile at all.


Check out String replaceAll() and the other replace(). And, I know I'm going to sound like a broken record here, but: Have you checked out regular expressions?
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Originally posted by Jeff Bosch:


Word is a String. String has no such method. Interesting that it would compile at all.


Check out String replaceAll() and the other replace(). And, I know I'm going to sound like a broken record here, but: Have you checked out regular expressions?


Actually, is not word.readline();

Is word = c.readLine(); but........

THANK YOU VERY MUCH!!!< FINALLY SOMEONE HELPS ME.

is replaceAll. I think with that i can change more than 1 character.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
As you only want to replace the suffix "or" with "our" and not all occurences anywhere in the word, once you get a word you can use String methods to test for the suffix and strip it off. Hint: the method name ends with "endsWith".

You'll find length() and substring(startPos, endPos) useful for getting pieces of a word. Note that indexes (character positions) in Strings are zero-based (first character is zero instead of one).

Using these, find a short way to replace the "or" suffix with "our". It should be just a few lines of code.

Finally, what are you supposed to do with the resulting words? Do that for each word and you're done!
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Originally posted by David Harkness:
As you only want to replace the suffix "or" with "our" and not all occurences anywhere in the word, once you get a word you can use String methods to test for the suffix and strip it off. Hint: the method name ends with "endsWith".

You'll find length() and substring(startPos, endPos) useful for getting pieces of a word. Note that indexes (character positions) in Strings are zero-based (first character is zero instead of one).

Using these, find a short way to replace the "or" suffix with "our". It should be just a few lines of code.

Finally, what are you supposed to do with the resulting words? Do that for each word and you're done!



OK, here is a problem, the program works perfectly. Here is my code:

String line,string;
c.println ("Enter a line of text");
line = c.readLine ();
StringTokenizer words = new StringTokenizer (line);
c.println ("The word is now: ");
//My loop to separate the words
while (words.hasMoreTokens ())
{
string = (words.nextToken
c.print(string.replaceAll("or","our")+" ");
}

But the program changes all the character he encountered on the words. For example when I type "for" the program output "four" and that's fine. But when I type "forever", the program types "fourever". I I only need to change the spelling of the words who 'ends' in or. What can I do to fix this?


BYE ALL (I think im starting to like JAVA)

[ April 22, 2005: Message edited by: Manuel Diaz ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Read my previous post. We both started replying near the same time.
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
Originally posted by David Harkness:
Read my previous post. We both started replying near the same time.


Ok, thank you, i see now. Dont go far!, maybe soon i will need more help. Im developing a PI Dartboard. Just kidding.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Originally posted by Manuel Diaz:

c.print(string.replaceAll("or","our")+" ");

But the program changes all the character he encountered on the words. For example when I type "for" the program output "four" and that's fine. But when I type "forever", the program types "fourever". I I only need to change the spelling of the words who 'ends' in or. What can I do to fix this?


Since you already parsed it into words, the replace is easy. You just need to replace only if it is at the end of the word. The "$" is used in regular expressions to signify the end of the data/line.

Hence... add the "$" to the search...

string.replaceAll("or$","our")

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

BTW, if you didn't break it up into words, you could have done the entire line like this:



Henry
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Or more simply:

The special sequence \b (or in a literal, "\\b") represents a word boundary - precisely what you need here.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing spelling 'or' to 'our'