• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with adding values to arraylist  RSS feed

 
Matrica Kovolunchy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I cannot figure out how to do this. I'm close, but the compiler keeps giving me the error:

C:\Users\ttr\Desktop>javac bttl.java
bttl.java:30: error: cannot find symbol
list.add(x);
^
symbol: variable list
location: class bttl
1 error

Here is the code, If any one would help me pinpoint the issue, I would be exceptionally happy! Thank you!

 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

list is defined as local variable which visible only within the method
if you want list to be accessible, you have to make it visible by either
- passing it as argument to createSquaresList
- declare it as static variable (visible within class)
 
Matrica Kovolunchy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I know how to declare "other" variables as static, but if I apply the static condition to the arraylist, the compiler gives me fits. I'm sure theres a better way to do it. But of all the ones I've tried (public, static, etc) none work. Thank you for your help!





bttl.java:9: error: illegal start of expression
static ArrayList<Double> list = createSquaresList(10);
^
1 error

 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect you have declare it within method which is invalid.
static variables should be declared as class level not within method.
 
Matrica Kovolunchy
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I putting the chips in. I can't get it to work. I tried adding it to the public class with no luck. I have no clue how to fix it on the variable declaration side of things. I'll try passing it in as a argument in the method. Thank you!
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I suggest you to use good naming practice. The class name bttl doesn't defines what this class needs to do.
Regarding the query, If this class is providing only utility services, you can do like below,
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!