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