Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

reading a file help..

0 views
Skip to first unread message

James

unread,
Sep 21, 2003, 1:58:00 PM9/21/03
to
Hi. I am having some problems reading in a file. I'm trying to read a file
called file.txt its contents are:
Jim
Brad

John
Dave
Paul

I can read in the first set of name up to the blank line, but when I reach
the blank line I get a error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at post.main(post.java:15)

which is telling me that I don't have a token on that line because its a
blank line. Can someone tell me how I can read in the file, so that when it
gets a blank line it skips it and reads in the next line? Below is my code,

public static void main (String[] args) throws Exception
{
BufferedReader reader = new BufferedReader (new FileReader("file.txt"));
String line;
Map map = new HashMap();

while ((line = reader.readLine()) != null)
{
StringTokenizer sTokenizer = new StringTokenizer(line);
String pName = sTokenizer.nextToken();
Set set = new TreeSet();
map.put (pName, set);
System.out.println(line);
}
}

Thanks for the help.


Gordon Beaton

unread,
Sep 21, 2003, 2:18:52 PM9/21/03
to
On Sun, 21 Sep 2003 17:58:00 GMT, James wrote:
> I can read in the first set of name up to the blank line, but when I
> reach the blank line I get a error:
>
> Exception in thread "main" java.util.NoSuchElementException
> at java.util.StringTokenizer.nextToken(Unknown Source)
> at post.main(post.java:15)
>
> which is telling me that I don't have a token on that line because
> its a blank line. Can someone tell me how I can read in the file, so
> that when it gets a blank line it skips it and reads in the next
> line? Below is my code,

As you've already figured out, the problem is that you assume (without
checking) that sTokenizer.nextToken() will succeed, but calling
nextToken() when there are no more tokens will cause the exception
you're getting.

You could use one of the String methods to remove leading and trailing
spaces before creating the StringTokenizer, but StringTokenizer itself
can tell you things like

- how many tokens there are in total
- whether or not there is a next token

I suggest you look more closely at the API documentation for
StringTokenizer to decide what test you need to do.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e

Chiron Paixos

unread,
Sep 21, 2003, 3:11:56 PM9/21/03
to
Hi,
perhaps inserting something like
System.out.println(line.length());
as the first statement inside your while-loop will give you an idea...

If not, ask again...

Roedy Green

unread,
Sep 21, 2003, 3:42:19 PM9/21/03
to
On Sun, 21 Sep 2003 17:58:00 GMT, "James" <dev...@linuxmail.org> wrote
or quoted :

>which is telling me that I don't have a token on that line because its a
>blank line. Can someone tell me how I can read in the file, so that when it
>gets a blank line it skips it and reads in the next line? Below is my code,

just do a loop of readLines. trim each line. If the length is 0, it
is a blank line. If the line in null, you hit eof.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

0 new messages