Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String analyzing

 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the best way to edit strings?
Edit as in delete, move and use as separate strings.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12181
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for everyone's help!
The program is not finished yet, but I'm working on it, and know exactly what to do.
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic