Newbie I/O problem
13/01/2018 12:08
Hello,I have a file in which i have points coordonnates, each line contains 3values , 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 motreturns the wole line instead of each value, i don't understand why, doessomeone 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(); }

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
13/01/2018 12:08 - 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 thesame issue as the old character stream vs. byte stream that led tojava.util.Date shrinking into something of a stub class, and theuse of Readers instead of DataInputStreams, etc.


I even heard that, in Thai, a sentence consists of words but thereis no space (white space) between words. (Imagine the kind of codeBreakIterator would load in a Thai locale in response toBrakIterator.getWordInstance()!)
My guess is that the gurus want us to use BreakIterator instead ofStringTokenizer for parsing natural languages, and StreamTokenizerto parse program code. My other guess is that they are probably rightabout this.... :)
Regards, Tony Dahlman---------------------------------------a (no spam)d ahlman( a t )att global( d o t )ne t

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
13/01/2018 12:08 -
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.


Yep.


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.


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



Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
13/01/2018 12:08 - You forgot the space between the quotes.


Also, invoking readLine() three timestwice in the loop and once in the while clause?
That would mean you only want every third line?And you are not checking for null ...


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


while ((chaine = b.readLine())!= null) {
//chaine = b.readLine(); o_O String[] sa = chaine.split(s+); for (int i=0;i<sa.length;++i) { System.out.println(sa[i]); } //chaine = b.readLine(); o_O }
If chaine includes leading spaces the first element in sa[] will be an empty string.


Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
13/01/2018 12:08 -
Indeed, that seems to have been added in 1.4.2. What a shame...



Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
13/01/2018 12:08 - I do appreciate those comments. Thanks.


As for simple data transfer and exchange formats which convey informationusing 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. benefitratio has been exceeded way beyond what is reasonable and sensible.


Finally no, your opinion is worth far more than a casual nod and there shouldbe 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

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
13/01/2018 12:08 - Chris Smith <cdsmith@twu.net> wrote innews:MPG.1a59fb381d557203989858@news.pop4.net:
The quote was from
http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
13/01/2018 12:08 -
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.



Source is Usenet: comp.lang.java.help
Sign in to add a comment

eDiscover
Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover
  
Categories

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below