Click to See Complete Forum and Search --> : Problem with StringTokenizer. String's split method is preferable.


Mr. Ram
05-11-2006, 06:00 AM
import java.util.StringTokenizer;

public class strr
{
public static void main(String args[])
{
String str = "RAMA&RAO&&SHIVA&&GOPI&KRISHNA";

StringTokenizer st = new StringTokenizer(str, "&&");

while(st.hasMoreTokens())
System.out.println(st.nextToken());

System.out.println("\n\n");

String strs[] = str.split("&&");

for(int i=0; i<strs.length; i++)
System.out.println(strs[i]);
}
}

Output :- :)

RAMA
RAO
SHIVA
GOPI
KRISHNA



RAMA&RAO
SHIVA
GOPI&KRISHNA

Cytael
05-15-2006, 12:18 AM
First, please use [ code ]or [ php ] blocks around your code while posting on the boards. It makes your code a lot easier to read.

Second, a detailed description of what problems you're having would be nice. What's your code not doing that it's supposed to do, or what's it doing that it shouldn't be doing?

Third, it looks to me like the code you posted is behaving properly: the .split method is splitting based on the "&&", which I'm gonna assume is the intended result. StringTokenizer is deprecated in favor of .split, and you shouldn't use it anyway.