a method/class that finds commas
Author |
Message |
krishon
|
Posted: Sat Dec 06, 2003 12:23 pm Post subject: a method/class that finds commas |
|
|
i'm doin io stuff now....i have read in lines and break stuff up where there are commas. I remember my teacher telling me there is some class or method or somethin that detects the instance of a comma, does anyone know what it is?
thx |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dan
|
Posted: Sun Dec 07, 2003 1:22 am Post subject: (No subject) |
|
|
i whould do somting like
code: |
for(int i = 0; i < yourString.length(); i++)
{
if(yourString.charAt(i - 1) == ',')
{
//what to do if comman hit
}
}
|
not shure if that works 100%, dont have java comaipering instaled right on this comp. if it dose not wrok check spelling and may be mess with indexes of for loop and string. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
rizzix
|
|
|
|
|
krishon
|
Posted: Sun Dec 07, 2003 11:01 am Post subject: (No subject) |
|
|
yah...i've looked at the api but i dunno where it is. urs just goes to the beginning of the api. that's because its all frames and stuff....so could u just post like wut to click...e.g. java.lang>>string etc. thx |
|
|
|
|
|
rizzix
|
|
|
|
|
krishon
|
Posted: Sun Dec 07, 2003 7:40 pm Post subject: (No subject) |
|
|
THAT'S THE ONE...just dunno which method, lol |
|
|
|
|
|
yuethomas
|
Posted: Mon Dec 08, 2003 9:30 pm Post subject: (No subject) |
|
|
Depending on your needs, either StringTokenizer or StreamTokenizer will satisfy you. If you've already read the contents of your input file (you mentioned you were doing I/O) into a string, then you should put it through StringTokenizer to generate tokens. However, if you want to streamline the process and just parse the input into tokens during file input, you might want to wrap the input stream (be it a FileReader, whatever) around a StreamTokenizer, like this:
code: | StreamTokenizer st = new StreamTokenizer (new BufferedReader (new FileReader ("filename"))); |
But if you just want to parse a string into tokens, then the following will suffice:
code: | String a = new String ("a b c d e f g");
String splitter = new String (" "); // specify the delimiter here
StringTokenizer token = new StringTokenizer (a);
while (token.hasMoreTokens ())
System.out.println (token.nextToken (splitter)); |
However, StringTokenizer is now considered a legacy class and is only retained for compatibility purposes. You could try using String's split method:
code: | String a = new String ("a b c d e f g");
String splitter = new String (" "); // specify the delimiter here
String[] tokens = a.split (splitter);
for (int i = 0; i <= tokens.length - 1; i ++)
System.out.println (tokens[i]);
|
|
|
|
|
|
|
|
|