This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes String analyzing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String analyzing" Watch "String analyzing" New topic
Author

String analyzing

Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

What is the best way to analyze a string?
I have a string that contains three words. Using if statements, I want the words to have different meanings in different contexts.

In the order each happens, I want the method that analyze the string to do the following:
1. Verify that the string is in the required format (two or three words, if two then the first has only numbers, if three then the two first words have only letters (To prevent bugs. Can use catch blocks instead)).
2. Store the last word in another string, and then delete it from the original string.
3. Store the first word in another string, then check if the string has two (false) or three (true) words.
4. If the verifying process returns true, verify something using the first word, and then delete said word from the original string. Then run the original string through if statements and get desired result.
4. If the verifying process returns false, store the original string in another string from another class, using the string with the last word to tell which string.

Damn, that was tricky to write .

Thanks in advance,
Eden
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

Eden Landau wrote:
Damn, that was tricky to write .

Indeed, and even trickier to understand
Perhaps you want to rephrase it? An example would definitely help us understand what problem you are trying to solver and where you are stuck.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

What's the best way to edit strings?
Edit as in delete, move and use as separate strings.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Use regex for pattern matching, either String.replace() or String.subString() methods for deleting a part of the String. Look for methods in java.lang.String API.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7486
    
  18

What is the best way to analyze a string?

John covered quite a lot of the useful methods; but in your case I'd also look at String.split(). Assuming that your "words" are whitespace-delimited, the usual form is:
String[] words = myString.split("\\s+");

and once you have an array of words, the rest should be plain sailing.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11150
    
  16

Eden Landau wrote:What's the best way to edit strings?
Edit as in delete, move and use as separate strings.

Remember that Strings are immutable. You cannot delete or change them in any way. all you can do is create new strings that may be made up from parts of the original.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

Thanks for everyone's help!
The program is not finished yet, but I'm working on it, and know exactly what to do.
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String analyzing
 
Similar Threads
I wanted to write a method that would return the year with the most number of hits as part of ex 4.3
java
trim() method
Sun Cirtification