Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 a method/class that finds commas
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
krishon




PostPosted: 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
Sponsor
sponsor
Dan




PostPosted: 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




PostPosted: Sun Dec 07, 2003 1:36 am   Post subject: (No subject)

there is one class, although i haven't used it so i can't give u much help there, but i know its does something similar: http://java.sun.com/j2se/1.4.2/docs/api/index.html
krishon




PostPosted: 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




PostPosted: Sun Dec 07, 2003 7:35 pm   Post subject: (No subject)

woops... heh http://java.sun.com/j2se/1.4.2/docs/api/java/io/StreamTokenizer.html
krishon




PostPosted: Sun Dec 07, 2003 7:40 pm   Post subject: (No subject)

THAT'S THE ONE...just dunno which method, lol
yuethomas




PostPosted: 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]);
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: