• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question About String Tokenizer  RSS feed

 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

I am having difficulty using the String tokenizer and the String buffer class together.

Let me be specific, I am trying to search a String, if it has the word "and", I want everything on the right side of "and" to be one String, and the other side to be a totally different string. I thought about using an array of StringBuffer, but i cannot correctly create a new StringBuffer in the while loop "while(st.hasMoreTokens)".

Any suggestions?

Here is some of my code, the problem i am having is in the doEvaluation method. compiles and runs fine,I just cannot split the Sring to make 2 new Strings.


Thank you

-Sean
[ March 18, 2005: Message edited by: Sean Magee ]
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the API for StringTokenizer, particularly at the constructors, you will see one that will help you. In your case, the "delim" sounds like it would be the word "and".

I think you are going down the wrong path trying to combine StringTokenizer and StringBuffer.
 
Avisesh Jain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor of StringTokeniser would be of no use because what ever string is passed as delimiter in the constructor, each individual character in the delimiter string will serve as delimiter.

So for example consider the code

String str ="john and anderson";
String Delim="and";
StringTokeniser st = new StringTokeniser(str, delim);
The different tokens are
joh
derso

So one suggestion i would give is continue with your approach,till u get the token as "and" keep concatenating the tokens once ur token is "and" put the concatenated string in a buffer increment the index of the buffer. At the end of the loop your buffer will have the desired tokens.
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
put the concatenated string in a buffer increment the index of the buffer



Could you elaborate what you mean here? I dont follow
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's an easier way to do this. Take a look at the JavaDocs for the String class and you will see an index() of method that returns the index of the first char in a String you are searching for. Once you have that index, you can use two calls to the substring() method to get the two halves of the String. Doing it this way will take 3 lines of code.

Layne
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Layne!

However, i do not understand the indexOf() method, i understand it searches for a character, but how would i search for the word "and", "or" and multiple other words in the same string, eventually this program will become really robust. seems to me indexOf() will only saerch for a char and return an integer value, i dont think this is what i am loking for, unless I do not understand the indexOf() method correctly.

Thanks,

-Sean
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several overloaded versions of indexOf(). You just happen to be looking at the first one of these that is listed. If you look further down, you will see that another one takes a String argument to search for a whole String rather than just a single character.

Layne
 
Ravi Srinivas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just a quick note that using this method (getting the index by using indexOf) you may still face the same problem that Avisesh mentioned earlier. For e.g. if the input string is "roland and john", using indexOf("and") to split it into two parts gives "rol" & " and john".

Ravi
 
Avisesh Jain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i shall try my best to make it clear.


String result[]= new String[50];
String temp = "";
int count = 0;
while(st.hasMoreTokens())
{
token = st.nextToken();
if(token.equals("and"))
{
result[count]=temp;
count++;
temp="";
}
else
{
temp= temp+token;//Concatenate
}
}

Is my logic clear? If not, do get back to me.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After a quick glance, it looks okay to me. I'd like to point out that we would be able to read your code more easily if you use UBB code tags. They will preserve your formatting, such as indentation.

I have one minor change to suggest. Instead of

user


The former version has the potential to throw a NullPointerException, but since "and" is a legitimate String object, you can use the former to avoid this situation. Instead, it will simply return false, IIRC.

So, does this code compile? More importantly does it produce the results you expect? If not, how do the actual results differ from your expectations? If you have problems, please post some more information, including answers to these questions. We'll be glad to help you figure it out.

Keep Coding!

Layne
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this helps people to be able to udnerstand my problem more clearly.

Lets say I have the String " I am in the room and the light is on "

When this is run through the program, it is essential that i have the String " I am in the room" and have the String " the light is on ".

Likewise with "or" and many other delimiters i will be implementing in the future.

So, if i had " I was in the Room and the Light is on or I go play outside"

Should break up into

String 1 : " I was in the Room"
String 2 : " the Light is on "
String 3 : " I go play outside "

I still have not found a solution to this, thanks for anyone that can help.

Avisesh Jain, you have helped me clear a lot of my logic erros, thank you for that.
However, in the previous code you put up above, the code on the left side of the String does get slotted into the array, since there is only one "and"
only the right side gets inside the array.


-Sean Magee
[ March 22, 2005: Message edited by: Sean Magee ]
 
Avisesh Jain
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sean, After the while loop you need to add one more line of code as follows.
result[count]=temp;

And the logic i have given would work with any number of delimiters u want, if u want to make it work for "or" also then add one more if condition and it will work

However, in the previous code you put up above, the code on the left side of the String does get slotted into the array, since there is only one "and"
only the right side gets inside the array.

Can you be more clear to what precisely your problem is?
 
Sean Magee
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avisesh, thanks a lot for your help. Sorry the previous post i wrote was really early in the morning, I meant only the left side gets insterted, not the right. However, everything works pretty nicely now. Since I am using JOptionPane stuff, for some reason a blank String gets created in the last slot of the array (not the last slot, but the last slot that is not null).

Thanks alot for your help and Layne's, I will be coninue to post the problems I have for this program. It is going to be a while before I complete it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!