File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Reading Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading Files" Watch "Reading Files" New topic

Reading Files

Vanessa Astle
Ranch Hand

Joined: May 08, 2007
Posts: 37
Hey, I'm only now noticing how picky the java file readers are. I'm using Scanner to scan a file in code below:

And the format for the file is:

But unless I'm very specific with the format of my file, the whole thing breaks. If I hit return after typing everything in my file, or if I press space after typing a single line, everything goes downhill.

I tried using the skip() method to skip patterns like a space or two new lines feeds in a row, but it doesn't seem to work. Can anyone suggest anything else I might try?

Thanks so much and have a nice day!
[ May 31, 2007: Message edited by: Vanessa Astle ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15087

Originally posted by Vanessa Astle:
Hey, I'm only now noticing how picky the java file readers are.

It's not that Java file readers are picky - computers are picky in general. A computer does exactly what you program it to do and nothing else, so you have to be very precise and think of every detail when you write a program. It's a good thing, imagine how hard it would be to program a computer that thinks by itself and guesses what you mean instead of doing exactly what you tell it to do...

You should add some error checking code at specific points to check if your variables contain what you expect. You could for example, after line.split("/"), check if you indeed got an array with two elements:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Reading Files
jQuery in Action, 3rd edition