This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i/o question

 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,
is there an io class besides Scanner that will parse the next() word?
i've used BufferedReader lately and have noticed a great improvement
in performance. is there a way i could implement this
behavior with BufferedReader or another class that isn't so top heavy.
any help will ...
thanks.

nikita
[ April 12, 2008: Message edited by: f. nikita thomas ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go through BufferedReader you find it will read from whichever stream you pass it. I think (not sure) Scanner uses a BufferedReader to read, then uses regular expressions to split the text into tokens.

You can use a BufferedReader to read, append it to a String (StringBuilder probably better), and then split with regular expressions (eg String.split()).

But I don't think BufferedReader will read a single token; Scanner is better for that. Probably easier to use, but there's nothing wrong with using BufferedReader.

You can find a file called src.zip in your Java installation folder; if you unzip that you can read the code of Scanner and check its details.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, it's new BufferedReader(new InputStreamReader(System.in)) or similar.

And Scanner does its own Exception handling.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Campbell,
it's just that if i use Scanner next():



i have to use this method one word at a time, apparently. i want to use BufferedReader and do this:



but one word at a time. any suggestions?
[ April 12, 2008: Message edited by: f. nikita thomas ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find it a bit difficult to understand the problem.

You can read a token (next()) or a whole line (readLine()) with a Scanner, using a regular expression as a delimiter. You could use punctutation as a delimiter, or even a combination of whitespace and punctuation, then the Scanner will read tokens back separated by punctuation, whitespace, etc.

You can append the result to a stringBuilder.

I think this will be an easier solution.

Go and find the Java Tutorials, and look for the bit about regular expressions. They're not as difficult to use as you think at first reading, but remember you need the \ escapes and you usually have to write \\ to get \.

Alternative: read the entire file into a single String, then split it with the String.split() method, again using punctuation as the regular expression.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a restriction which forces the input to be read one(1) word at a time. i looked at the source code for both BufferedReader and Scanner and i may be able to subclass BufferedReader to implement the next() method. i know i'm new at this, however i was just asking if there was a way to do this without losing a couple of nights sleep over it. otherwise i'd just use the first removepunct(). i'm doing a hash table assignment and wanted to optimize for speed. just want to be good, ya know?
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just an aside, the first method won't remove non letters or digits if they occur within the string(bob's); this is what my instructor wants. the second way whacks everything including whitespace.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I prefer the use of a StringBuilder.


But don't optimise for speed. Optimise for correctness, and let the compiler take care of the speed. As long as you don't use a known inefficient algorithm, you will probably get just as fast like that.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by algorithm do you mean the hash function or my own inept attempts at coding?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's inept about your coding?

By "algorithm" I meant using something like bubble sort for a large collection when merge sort or quicksort are available as alternatives.
 
f. nikita thomas
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, cool! thanks C.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic