File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes i/o question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i/o question" Watch "i/o question" New topic
Author

i/o question

f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
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 ]

Imagination is more important than knowledge "Albert Einstein"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
Actually, it's new BufferedReader(new InputStreamReader(System.in)) or similar.

And Scanner does its own Exception handling.
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Mar 02, 2008
Posts: 87
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

Joined: Mar 02, 2008
Posts: 87
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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Mar 02, 2008
Posts: 87
by algorithm do you mean the hash function or my own inept attempts at coding?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Mar 02, 2008
Posts: 87
oh, cool! thanks C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i/o question
 
Similar Threads
response.sendRedirect() behaviour
thanks
what exactly is groovy?
stringbuffer :nullpointerexception
Hibernate generate timestamp on create and update