• 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
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Why Arraylist gets cleard after being put to gether in a method?

 
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is where im putting all my methods:


i only have the two in there for now

Here is the menue that i use to call them with:



When i exit the the loadData method and try to print the array list i just made with the data from the txt. file . The array list has nothing in it when i try to print it out. it leave the load data method and clears the array lidt and i also tell it to return the array list.

here is another class i use to keep up with the word i get from the file to put into the array



I just don't understand why the array list goes blank whn it leaves the loadData method.
 
author & internet detective
Posts: 40678
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two sentence variables in this code.

One is in the loadData() method. It isn't returned so at the end of the message it is eligible for garbage collection.  The main() method has a separate sentence variable that isn't updated.

You can use a return value or a static variable to fix this.
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the return statement in the load method doesn't work in line 62 of the first chunk of code
 
Jeanne Boyarsky
author & internet detective
Posts: 40678
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because you don't store the result anywhere. See the difference between these two?



The first ignores the result. The second stores it in the sentence variable in the main() method.
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i actually caught that and fixed it an a few other things.Thanks for pointing it out i had to read your comment a few times for it to click what you was saying now i have a problem with when i try to print to the con sole all it give me is null back an not the strings i put in.
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay i do have a question on 34- 40  how would i get it to add the entire line to the array with out splitting it up Ive tried to do away with the tokenizer but i get tons of errors
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The basic loop for reading a file looks like this:
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

line is a String and sentence is an ArrayList<Character>.  Can you see why you can't add line to sentence?
 
Daniel Stallard
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sentence is not the actual array-list? I assume is what you are getting at.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, sentence is an ArrayList of Characters, not Strings.
 
You showed up just in time for the waffles! And this 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