• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • 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: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • 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
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic loop for reading a file looks like this:
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sentence is not the actual array-list? I assume is what you are getting at.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, sentence is an ArrayList of Characters, not Strings.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!