• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Book P570 populateList()

 
Conor Crowe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the format of your text file? Is it mandatory that each line will contain three Strings separated by a forward slash character?
 
Conor Crowe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Conor Crowe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic