AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

newbie I/O problem

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Samba
Guest





PostPosted: Thu Dec 25, 2003 10:14 pm    Post subject: newbie I/O problem Reply with quote



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





PostPosted: Fri Dec 26, 2003 5:57 am    Post subject: Re: newbie I/O problem Reply with quote



"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





PostPosted: Fri Dec 26, 2003 3:36 pm    Post subject: Re: newbie I/O problem Reply with quote



Thank you to both of you


Back to top
Chris Smith
Guest





PostPosted: Sun Dec 28, 2003 11:54 pm    Post subject: Re: newbie I/O problem Reply with quote

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





PostPosted: Mon Dec 29, 2003 3:50 pm    Post subject: Re: newbie I/O problem Reply with quote

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





PostPosted: Mon Dec 29, 2003 6:06 pm    Post subject: Re: newbie I/O problem Reply with quote

Thomas Schodt wrote:
Quote:
The quote was from

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

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





PostPosted: Tue Dec 30, 2003 5:13 am    Post subject: Re: newbie I/O problem Reply with quote

Chris Smith wrote:
Quote:

Thomas Schodt wrote:
The quote was from

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

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

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





PostPosted: Tue Dec 30, 2003 4:09 pm    Post subject: Re: newbie I/O problem Reply with quote

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

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





PostPosted: Wed Dec 31, 2003 5:02 am    Post subject: Re: newbie I/O problem Reply with quote

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





PostPosted: Fri Jan 02, 2004 5:03 pm    Post subject: Re: newbie I/O problem Reply with quote

"Chris Smith" <cdsmith (AT) twu (DOT) net> wrote

Quote:
Thomas Schodt wrote:
The quote was from

http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

Indeed, that seems to have been added in 1.4.2. What a shame...


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





PostPosted: Sat Jan 03, 2004 12:29 pm    Post subject: Re: newbie I/O problem Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.