Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Trying to change elements of a LinkedList Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Trying to change elements of a LinkedList" Watch "Trying to change elements of a LinkedList" New topic
Author

Trying to change elements of a LinkedList

Tyler Tallman
Greenhorn

Joined: Oct 04, 2012
Posts: 20


I am trying to figure out how to replace the different elements of ringBuffer with randomly generated numbers from Math.random(). I'm not supposed to return ringBuffer, so I'm not really sure how I'm supposed to access the ringBuffer created in public GuitarString so I can change the elements in it from public void pluck.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 792
    
    2

At line 19 in your constructor, you are actually hiding your field and initializing new ringBuffer as local to the constructor.

On the other hand, in pluck() method you are declaring double variable r and try to invoke method nextDouble() on it, which is wrong - double is a primitive type, not an object, so you can't invoke method on it. If you want to generate random values, check the API for Random class. I suppose you meant r to be instance of it.


The quieter you are, the more you are able to hear.
Tyler Tallman
Greenhorn

Joined: Oct 04, 2012
Posts: 20


Ok so if I have ringBuffer as a private class instead then it should be available to everything right? But when I try to change ringBuffer from pluck it just gives me the same thing.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5852
    
    6

Tyler Tallman wrote:

Ok so if I have ringBuffer as a private class instead then it should be available to everything right?


You currently have it as a private member variable (not a private class), just like you did before. The only difference is that now you're not hiding that member variable with a local variable of the same name. You could have achieved the same effect by changing

in the constructor to just


That way, instead of declaring a separate, local variable that hides the member variable, you're just referring to the member variable.

However, in this case it's preferable to initialize it at declaration as you're now doing.

But when I try to change ringBuffer from pluck it just gives me the same thing.


This is a separate error that has nothing to do with ringBuffer.

You're still conflating the Math.random() method and the java.util.Random class, and, as a result, you're still trying to call nextDouble() on a double primitive, which has no methods, and even if it did, would not have a nextDouble() that gives you random value. Use the java.util.Random class, not the Math.random() method.
Tyler Tallman
Greenhorn

Joined: Oct 04, 2012
Posts: 20
When I change Queue<Double> ringBuffer = new LinkedList<Double>() to ringBuffer = new LinkedList<Double>(), it tells me that there is <identifier> expected, which is why I had Queue<Double> in there
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5852
    
    6

Tyler Tallman wrote:When I change Queue<Double> ringBuffer = new LinkedList<Double>() to ringBuffer = new LinkedList<Double>(), it tells me that there is <identifier> expected, which is why I had Queue<Double> in there


I'm talking about inside the constructor.

In the class body, outside any constructor or method body, you have


That declares a member variable that is available inside any constructor or non-static method. There's a separate copy of the variable for each instance of GuitarString you create.

In that context, you can't just do ringBuffer = ....

Inside a constructor or method, if you have


you're declaring a local variable called ringBuffer that only exists as long as that constructor or method is executing. That variable hides the member variable, since it has the same name.

If, on the other hand, inside your constructor or method, you do:


since you haven't included a type (the Queue<Double> part), you're not declaring a local variable; you're just using the member variable.
Tyler Tallman
Greenhorn

Joined: Oct 04, 2012
Posts: 20
Ahh that actually makes sense to me now thanks for clearing that up. But I am having trouble making a call on pluck in my main method, I want to make sure that it alters ringBuffer in the way that I intended but... well I can't really do that without being able to make a call on it
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5852
    
    6

Tyler Tallman wrote:Ahh that actually makes sense to me now thanks for clearing that up. But I am having trouble making a call on pluck in my main method, I want to make sure that it alters ringBuffer in the way that I intended but... well I can't really do that without being able to make a call on it


Okay, and what exactly do you mean by "having trouble"? What is it that you're trying to do and exactly how is it not working?
Tyler Tallman
Greenhorn

Joined: Oct 04, 2012
Posts: 20
Jeff Verdegan wrote:
Tyler Tallman wrote:Ahh that actually makes sense to me now thanks for clearing that up. But I am having trouble making a call on pluck in my main method, I want to make sure that it alters ringBuffer in the way that I intended but... well I can't really do that without being able to make a call on it


Okay, and what exactly do you mean by "having trouble"? What is it that you're trying to do and exactly how is it not working?


When I try to make a call on pluck in main, it tells me that "non-static method pluck() cannot be referenced from a static context." I can't have pluck as a static method because it is a void, it may be that I can't call it or more likely I just don't know the syntax for it.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5852
    
    6

Tyler Tallman wrote:
When I try to make a call on pluck in main, it tells me that "non-static method pluck() cannot be referenced from a static context."


So you need to create an instance of your GuitarString class and call pluck() on that instance. You already have

in your main(), so you just need to call g.pluck(), instead of an unqualified pluck in a static context (which is what main()'s body is).

I can't have pluck as a static method because it is a void


Yes you can. Static methods can be declred void. For instance, main() is static and void. However, don't do that. Best to get in the habit of using objects rather than making everything static just to make the compiler happy.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5852
    
    6

Tyler Tallman wrote:
When I try to make a call on pluck in main


Also, in the future, rather than just describing your code and the error message, copy/paste the relevant bits of your code and the exact, complete error message. That will make it a lot easier for people to get a clear picture of what's going on.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Trying to change elements of a LinkedList
 
Similar Threads
compile error
Row & Column totals in jTable
incompatible types: found double required float error;
Using recursion to print alternating squares?
can anyone please help me spot my errors?