 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Samba Guest
|
Posted: Thu Dec 25, 2003 10:14 pm Post subject: newbie I/O problem |
|
|
Hello,
I have a file in which i have points coordonnates, each line contains 3
values , for instance:
-0.9364249 -0.3042628 -0.7692308E-01
-0.7965705 -0.5787425 -0.7692308E-01
-0.5787425 -0.7965705 -0.7692308E-01 ...
I need to get every value, here is the code that i use, my variable "mot"
returns the wole line instead of each value, i don't understand why, does
someone knows?
Thanks a lot,
String chaine, mot;
FileInputStream fichier = new FileInputStream("mon_fichier");
InputStreamReader entree = new InputStreamReader(fichier);
BufferedReader b = new BufferedReader(entree);
// Récupération liste de points
while ((chaine = b.readLine())!= null) {
System.out.println("Liste points");
chaine = b.readLine();
StringTokenizer st = new StringTokenizer(chaine, "");
mot = st.nextToken();
while (st.hasMoreTokens()){
System.out.println(mot);
mot = st.nextToken();
}
chaine = b.readLine();
}
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Fri Dec 26, 2003 5:57 am Post subject: Re: newbie I/O problem |
|
|
"Samba" <sambachat (AT) nshotmail (DOT) com> wrote
....
| Quote: | -0.9364249 -0.3042628 -0.7692308E-01
...
I need to get every value, here is the code that i use, my variable "mot"
returns the wole line instead of each value,
... |
| Quote: | StringTokenizer st = new StringTokenizer(chaine, "");
|
StringTokenizer st = new StringTokenizer(chaine, "*");
insert a space (' ') wher I put the '*'..
HTH
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
|
|
| Back to top |
|
 |
Samba Guest
|
Posted: Fri Dec 26, 2003 3:36 pm Post subject: Re: newbie I/O problem |
|
|
Thank you to both of you
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Sun Dec 28, 2003 11:54 pm Post subject: Re: newbie I/O problem |
|
|
Samba wrote:
| Quote: | I need to get every value, here is the code that i use, my variable "mot"
returns the wole line instead of each value, i don't understand why, does
someone knows?
StringTokenizer st = new StringTokenizer(chaine, "");
mot = st.nextToken();
|
You went out of your way to specify that there are no delimiters.
Without delimiters, StringTokenizer cannot break up the String. I don't
know why you thought this was necessary; if you explain, then perhaps I
can help clear up the confusion.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Mon Dec 29, 2003 3:50 pm Post subject: Re: newbie I/O problem |
|
|
Thomas Schodt wrote:
| Quote: | StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended that
anyone seeking this functionality use the split method of String or the
java.util.regex package instead.
|
Eh? By who? It's not deprecated, and there are no notes in the API
docs discouraging its use.
StringTokenizer is a nice, convenient abstraction for the task for which
it was intended: breaking text up by whitespace. Granted, its more
complex uses -- such as specifying non-whitespace delimiters, and
especially changing delimiters while tokenizing -- are often used in
poor code where a more flexible parsing mechanism ought to be used.
However, nothing beats the simple abstraction that is provided by
StringTokenizer if your task really is to break up Strings into
whitespace.
Regular expressions are not an excuse to neglect abstraction.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Mon Dec 29, 2003 6:06 pm Post subject: Re: newbie I/O problem |
|
|
Thomas Schodt wrote:
Indeed, that seems to have been added in 1.4.2. What a shame...
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Tony Dahlman Guest
|
Posted: Tue Dec 30, 2003 5:13 am Post subject: Re: newbie I/O problem |
|
|
Chris Smith wrote:
Chris,
What do you think of BreakIterator in java.text? Can't it do what
StringTokenizer does, but maybe better? I'm guessing this is the
same issue as the old character stream vs. byte stream that led to
java.util.Date shrinking into something of a stub class, and the
use of Readers instead of DataInputStreams, etc.
I even heard that, in Thai, a sentence consists of words but there
is no space (white space) between words. (Imagine the kind of code
BreakIterator would load in a Thai locale in response to
BrakIterator.getWordInstance()!)
My guess is that the gurus want us to use BreakIterator instead of
StringTokenizer for parsing natural languages, and StreamTokenizer
to parse program code. My other guess is that they are probably right
about this.... :)
Regards, Tony Dahlman
---------------------------------------
a (no spam)d ahlman( a t )att global( d o t )ne t
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Tue Dec 30, 2003 4:09 pm Post subject: Re: newbie I/O problem |
|
|
Tony Dahlman wrote:
| Quote: | What do you think of BreakIterator in java.text? Can't it do what
StringTokenizer does, but maybe better? I'm guessing this is the
same issue as the old character stream vs. byte stream that led to
java.util.Date shrinking into something of a stub class, and the
use of Readers instead of DataInputStreams, etc.
|
BreakIterator is great for breaking up natural language text... a task
for which StringTokenizer was never well-suited. However, it's
obviously the wrong choice for a very large class of whitespace-
delimited machine-readable formats for which StringTokenizer is uniquely
suited.
| Quote: | My guess is that the gurus want us to use BreakIterator instead of
StringTokenizer for parsing natural languages,
|
Yep.
| Quote: | and StreamTokenizer to parse program code.
|
Well, sorta. There is, in any case, a fairly sizable number of
programming languages that can be conveniently lexed with
StreamTokenizer; notably including Java. I'd be reticent to lump all
"program code" into that category, though, even for the strictest
definition of "program code".
That said, I've never been a fan of StreamTokenizer. I tend to feel
that in situatuons where it's appropriate, it's actually rather more
appropriate to build a full-fledged lexer using a utility like Cup or
JavaCC.
| Quote: | My other guess is that they are probably right
about this....
|
But you've left out the primary applications of StringTokenizer...
particularly, simple data transfer and exchange formats which convey
information using whitespace delimiting, or other fixed delimiters where
data is guaranteed to be present. These are the tasks for which the
"powers that be" are trying to push direct applications of regular
expressions along with String.split... and they are wrong, IMHO (the H
is questionable here).
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Tony Dahlman Guest
|
Posted: Wed Dec 31, 2003 5:02 am Post subject: Re: newbie I/O problem |
|
|
Chris Smith wrote:
| Quote: |
Tony Dahlman wrote:
What do you think of BreakIterator in java.text? Can't it do what
StringTokenizer does, but maybe better? I'm guessing this is the
same issue as the old character stream vs. byte stream that led to
java.util.Date shrinking into something of a stub class, and the
use of Readers instead of DataInputStreams, etc.
BreakIterator is great for breaking up natural language text... a task
for which StringTokenizer was never well-suited. However, it's
obviously the wrong choice for a very large class of whitespace-
delimited machine-readable formats for which StringTokenizer is uniquely
suited.
My guess is that the gurus want us to use BreakIterator instead of
StringTokenizer for parsing natural languages,
Yep.
and StreamTokenizer to parse program code.
Well, sorta. There is, in any case, a fairly sizable number of
programming languages that can be conveniently lexed with
StreamTokenizer; notably including Java. I'd be reticent to lump all
"program code" into that category, though, even for the strictest
definition of "program code".
That said, I've never been a fan of StreamTokenizer. I tend to feel
that in situatuons where it's appropriate, it's actually rather more
appropriate to build a full-fledged lexer using a utility like Cup or
JavaCC.
My other guess is that they are probably right
about this.... :)
But you've left out the primary applications of StringTokenizer...
particularly, simple data transfer and exchange formats which convey
information using whitespace delimiting, or other fixed delimiters where
data is guaranteed to be present. These are the tasks for which the
"powers that be" are trying to push direct applications of regular
expressions along with String.split... and they are wrong, IMHO (the H
is questionable here).
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
I do appreciate those comments. Thanks.
As for "simple data transfer and exchange formats which convey information
using whitespace delimiting..." I admit to no experience there.
And certainly I agree that if people are using regular expressions and
String.split() to parse incoming data streams, the bloat vs. benefit
ratio has been exceeded way beyond what is reasonable and sensible.
Finally no, your opinion is worth far more than a casual nod and there should
be nothing (H)umble about your views on Java coding.
Regards, Tony Dahlman
---------------------------------------
a (no spam)d ahlman( a t )att global( d o t )ne t
|
|
| Back to top |
|
 |
Dale King Guest
|
Posted: Fri Jan 02, 2004 5:03 pm Post subject: Re: newbie I/O problem |
|
|
"Chris Smith" <cdsmith (AT) twu (DOT) net> wrote
Seems like a good thing to discourage it to me. It leads to no end of
confusion as it is difficult to use for parsing CSV files and the use of a
String argument for delimiters leads many newbies into thinking that it uses
a sequence of characters for delimiters rather than a set of characters.
And worst of all much of its functionality cannot be relied upon, because it
behaves differently for different versions of the library. I was bit by that
one myself. For instance, consider the following code:
StringTokenizer st = new StringTokenizer("test=abcd efg hij", "=");
System.out.println(st.nextToken());
while (st.hasMoreTokens())
{
System.out.println(st.nextToken(" "));
}
Output before JDK1.3
test
abcd
efg
hij
Output starting with JDK1.3
test
=abcd
efg
hij
--
Dale King
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Sat Jan 03, 2004 12:29 pm Post subject: Re: newbie I/O problem |
|
|
Dale King wrote:
| Quote: | Seems like a good thing to discourage it to me. It leads to no end of
confusion as it is difficult to use for parsing CSV files and the use of a
String argument for delimiters leads many newbies into thinking that it uses
a sequence of characters for delimiters rather than a set of characters.
And worst of all much of its functionality cannot be relied upon, because it
behaves differently for different versions of the library.
|
I definitely agree it's dangerous when StringTokenizer is stretched to
accomodate more complex parsing needs. I just don't think there's a
good replacement for its basic uses. It could also be very easily
extended to some other uses in a backward-compatible way (something that
I suppose will never happen if Sun is looking at StringTokenizer as a
"legacy" class).
Sure, it will never handle something like CSV (which, with quoting, is
not a trivial format), but I would like to see the following changes:
- Deprecate the nextToken(String) method, which is confusing anyway.
- Add a constructor with a flag to return empty tokens.
- Remove that silly notice about only using it in legacy code.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|