Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to add elements from a file into an Array List

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone

I wanted to read a file and then add the contents of the file into an ArrayList.



and then I also had a catch following try. But when I say cr.add() it is not adding . I have also tried to add using:



Can someone give me any idea of doing it.

Thank you.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When you post code, don't double-click the code button. Make sure your source code is in between a [code] start tag and [/code] end tag. I've fixed your post for you.

About your problem: How do you know "it is not adding"? Does your code compile? Does it run? Do you get any error messages? Does the program do something else than you expected? If so, then what did you expect exactly, and how does the actual output differ from that?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Even I have the same problem .. IT is giving me a Number format exception. Can someone please help.

Thanks!!
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that you are using token multiple times in line 22. Are you sure that is correct? It's the same string that you are passing to the constructor multiple times. I think you meant to read multiple different tokens from the scanner and pass those for the individual parameters for the constructor.

By the way, why is it that Karthik surya posted the original question, and Divya Kotamraju answers? Are you one person with two accounts? (Oh, now you've edited your answer as if you're someone else???)
 
Divya Kotamraju
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jasper,

No I am not the same person as Karthik. Even I am doing similar kind of assignment. So I have posted as a reply to this particular post.
 
Karthik surya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jasper,

No. and regarding this question it is giving me number format exception. I think token contains the next value of the scanned i/p. So I have used it.

Thanks!!
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik surya wrote:No. and regarding this question it is giving me number format exception. I think token contains the next value of the scanned i/p. So I have used it.

Take a look at Jesper's first post again. He asked you about seven questions, but you only answered one. The others will give you some key clues to solving the problems with your code.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic