Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading/parsing file  RSS feed

 
Aman Dhillon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to read and parse through a file called exampleData.txt which looks like as follows:

Read|Books I have read
Want to Read|Books I want to read
Computers|Computers, IT, Computer Science, IT and Society books
Fiction|
Non-Fiction|
####
Catch-22|Joseph Heller|1961#Fiction|Read
Clearing The Plains|James Daschuk|2013#Read|Non-Fiction
How to Create a Mind|Ray Kurweil|2012#Computers|Read
Dirk Gentlyís Holistic Detective Agency|Douglas Adams|1987#
####
Catch-22|2015-11-03 09:01:16
Quite possibly the best work of fiction ever.
Heller captures the aburdity of life in a way no other author
ever has.
#
How to Create a Mind|2015-11-03 09:01:16
A must read for anyone interested in AI.
#



This is my code:


I'm trying to read in the first couple of tokens just to see if it'll work, but for some reason when I run the method, it doesn't work. I used the debugger feature on my IDE (BlueJ) and it goes into the file, but it never actually reads in the first token (i.e. String shelfName = scan.next();). I'm not sure what my issue is.

Please let me know if anyone can help me with this.
Thanks
 
Simon Roberts
Author
Ranch Hand
Posts: 177
9
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to JavaRanch.

One of the things you'll probably want to do is to format your code as code. In the editor window (JavaRanch's editor, that is) select the program code, and hit the "Code" button you see at the top. Then it won't toss out all your nice formatting.

Second, you import java.io.*, but appear to be defining a class called FileReader. There's a FileReader class in the io package anyway, so you might want to avoid that name

My usual approach to this kind of thing is to simplify the text while I test it out. It's really easy to get confused about the delimiters, and if you start with something simpler (e.g the pipe by itself) it might start behaving, then you can improve the delimiter incrementally.

Also, it looks like you're using the scanner to split the line on the pipe character, but then trying to split it again, using the split method of String.

(Also, when you say "it never reads" what does it do? Block? Quit out entirely? Throw an exception? Some more detail might (or might not!) be helpful.)

Not sure if that helps, but...
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

One of the things you'll probably want to do is to format your code as code.

We have a FAQ on that: UseCodeTags (that's a link). I'll do it for you this time.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the pipe (|) is a meta-character for regular expressions, so it has to be escaped:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!