• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

K&B Book P570 populateList()

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic