It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Book P570 populateList() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Book P570 populateList()" Watch "K&B Book P570 populateList()" New topic
Author

K&B Book P570 populateList()

Conor Crowe
Greenhorn

Joined: Mar 03, 2011
Posts: 3
Hello,

I am trying to read in from a text file and enter the data into an ArrayList.
When I tokenize the data read in from the file, there seems to be a whitespace read in which causes an array out of bounds exception when the if clause (tokens.length ==3) is not used.
You can see from the output that an array of size 1 is read at the end of file.
Could someone please explain why it does this?

Thank you




output:

Donnie Darko
sci-fi
Gyllenhall, Jake
3
Raiders of the Lost Ark
action
Ford, Harrison
3
2001
sci-fi
??
3
Caddy Shack
comedy
Murray, Bill
3
Star Wars
sci-fi
Ford, Harrison
3
Lost in Translation
comedy
Murray, Bill
3
Patriot Games
action
Ford, Harrison
3

1
[Donnie Darko sci-fi Gyllenhall, Jake
, Raiders of the Lost Ark action Ford, Harrison
, 2001 sci-fi ??
, Caddy Shack comedy Murray, Bill
, Star Wars sci-fi Ford, Harrison
, Lost in Translation comedy Murray, Bill
, Patriot Games action Ford, Harrison
]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

What is the format of your text file? Is it mandatory that each line will contain three Strings separated by a forward slash character?


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Conor Crowe
Greenhorn

Joined: Mar 03, 2011
Posts: 3
The file copied straight from the K&B book is as follows:

Donnie Darko/sci-fi/Gyllenhall, Jake
Raiders of the Lost Ark/action/Ford, Harrison
2001/sci-fi/??
Caddy Shack/comedy/Murray, Bill
Star Wars/sci-fi/Ford, Harrison
Lost in Translation/comedy/Murray, Bill
Patriot Games/action/Ford, Harrison

I suppose what I am really asking is why is a String read after the buffered reader reads a null value ?

when I print out the String values there are eight lines output but only seven lines in the file.
There are seven with length 3 and one with length 1.
Output:

Donnie Darko
sci-fi
Gyllenhall, Jake
3
Raiders of the Lost Ark
action
Ford, Harrison
3
2001
sci-fi
??
3
Caddy Shack
comedy
Murray, Bill
3
Star Wars
sci-fi
Ford, Harrison
3
Lost in Translation
comedy
Murray, Bill
3
Patriot Games
action
Ford, Harrison
3

1

Thanks
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
You get a "1" at the end , because you typed an ENTER after last line in file dvdinfo.txt
Double check your file to be sure there is no newline char after last line.


SCJP 6.0 (88%)
Conor Crowe
Greenhorn

Joined: Mar 03, 2011
Posts: 3
Thank you Dragos

I could not figure out how to remove or check if there is a new line character at the end of the file.
I changed the while loop as follows:



Kurt Zhu
Greenhorn

Joined: Aug 25, 2011
Posts: 8
Conor Crowe wrote:Hello,

I am trying to read in from a text file and enter the data into an ArrayList.
When I tokenize the data read in from the file, there seems to be a whitespace read in which causes an array out of bounds exception when the if clause (tokens.length ==3) is not used.
You can see from the output that an array of size 1 is read at the end of file.
Could someone please explain why it does this?

Thank you




output:

Donnie Darko
sci-fi
Gyllenhall, Jake
3
Raiders of the Lost Ark
action
Ford, Harrison
3
2001
sci-fi
??
3
Caddy Shack
comedy
Murray, Bill
3
Star Wars
sci-fi
Ford, Harrison
3
Lost in Translation
comedy
Murray, Bill
3
Patriot Games
action
Ford, Harrison
3

1
[Donnie Darko sci-fi Gyllenhall, Jake
, Raiders of the Lost Ark action Ford, Harrison
, 2001 sci-fi ??
, Caddy Shack comedy Murray, Bill
, Star Wars sci-fi Ford, Harrison
, Lost in Translation comedy Murray, Bill
, Patriot Games action Ford, Harrison
]


Thank you for your sharing.
I think I don't have to go as deep for understanding the topic in the SCJP study guide.
What I need is how I can implement
[Donnie Darko sci-fi Gyllenhall, Jake
, Raiders of the Lost Ark action Ford, Harrison
, 2001 sci-fi ??
, Caddy Shack comedy Murray, Bill
, Star Wars sci-fi Ford, Harrison
, Lost in Translation comedy Murray, Bill
, Patriot Games action Ford, Harrison
]
I borrowed your code in below class, which worked well. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Book P570 populateList()
 
Similar Threads
what sorting algorithm comparator,comparable does?
Putting an object instance into an ArrayList
Putting an object instance into an ArrayList
Sorting Collections Doubt
Confused with output of program using Scanner class