Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

StreamTokenizer question

Thread view: 
Philippe - 04 Jun 2007 19:47 GMT
Hi all.

I'm trying to read a file with many entry.  Thus, the first colum is a date.
Here's a row example :

08/22/05 15:18:47 21.00 0.00 0.00 -0.113 -1861.3 397.7 169.4 38.8 8.7

When I'm reading the first token (I'm using StreamTokenizer class) the first
token is 08.0.  It looks like he consider the first one as a number.  I
would like to ead the first token as 08/22/05.

I tryed to use st.ordinaryChar('/'); but it dont hlep me.

So my question is, how I can read the first token as a string instead of 3
oken considered as numerical.

Best regards,
Phil
Hunter Gratzner - 04 Jun 2007 23:32 GMT
> 08/22/05 15:18:47 21.00 0.00 0.00 -0.113 -1861.3 397.7 169.4 38.8 8.7
>
> When I'm reading the first token (I'm using StreamTokenizer class)

StreamTokenizer is good for nothing. Use String.split("\\s+") on each
input line.
Arne Vajhøj - 05 Jun 2007 01:48 GMT
> I'm trying to read a file with many entry.  Thus, the first colum is a date.
> Here's a row example :
[quoted text clipped - 9 lines]
> So my question is, how I can read the first token as a string instead of 3
> oken considered as numerical.

Try look at this code:

package june;

import java.util.StringTokenizer;

public class ST {
    public static void main(String[] args) {
        String s = "08/22/05 15:18:47 21.00 0.00 0.00 -0.113 -1861.3
397.7 169.4 38.8 8.7";
        StringTokenizer st = new StringTokenizer(s);
        while(st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }
        String[] parts = s.split(" ");
        for(int i = 0; i < parts.length; i++) {
            System.out.println(parts[i]);
        }
    }
}

You can easily parse the strings to int or Date if needed.

Arne
Lew - 05 Jun 2007 03:40 GMT
> Try look at this code:
>
[quoted text clipped - 18 lines]
>
> You can easily parse the strings to int or Date if needed.

Try looking at this information, also:
<http://en.wikipedia.org/wiki/Cross-posting>
> This is distinct from 'double posting' [a.k.a. "multi-posting"] which involves posting multiple messages, each posted to a single forum, newsgroup, or topic area.

and
<http://en.wikipedia.org/wiki/Top-posting#Top-posting>

These groups generally respond better to "interleaved posting", also called
"inline posting".

Signature

Lew

Juan Pedro Villa - 05 Jun 2007 03:54 GMT
On Jun 4, 7:48 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> package june;
> import java.util.StringTokenizer;
[quoted text clipped - 11 lines]
>      }
> }

The split method throws PatternSyntaxException if the regular
expression's syntax is invalid. It doesn't change much, but I prefer
to consider the possibility of catching the exception:

import java.util.regex.PatternSyntaxException;

public class Main {

   public static void main(String[] args) {
    String str = "08/22/05 15:18:47 21.00 0.00 0.00 " +
       "-0.113 -1861.3 397.7 169.4 38.8 8.7";
    try {
       String[] tokens = str.split(" ");
       for (int i = 0; i < tokens.length; i++) {
        System.out.println(tokens[i]);
       }
    } catch (PatternSyntaxException e) {
       System.err.println(e.getMessage());
    }
   }

}

The result of this little program is:

08/22/05
15:18:47
21.00
0.00
0.00
-0.113
-1861.3
397.7
169.4
38.8
8.7

And, as Arne said, you can deal with that if you need to.

Regards,
Juan Pedro Villa
Arne Vajhøj - 06 Jun 2007 00:44 GMT
> The split method throws PatternSyntaxException if the regular
> expression's syntax is invalid. It doesn't change much, but I prefer
> to consider the possibility of catching the exception:

>     try {
>        String[] tokens = str.split(" ");

>     } catch (PatternSyntaxException e) {
>        System.err.println(e.getMessage());
>     }

Considering that the pattern is hardcoded, then that could
be considered an overkill.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.