StreamTokenizer question

P

Philippe

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
 
H

Hunter Gratzner

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.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Philippe said:
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.

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);
}
}
}

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

Arne
 
L

Lew

Arne said:
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);
}
}
}

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


Try looking at this information, also:
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".
 
J

Juan Pedro Villa

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);
}
}
}


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);
}
} 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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Juan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top