aspose file tools*
The moose likes Beginning Java and the fly likes Running method on object problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running method on object problem" Watch "Running method on object problem" New topic
Author

Running method on object problem

Jack Fletcher
Greenhorn

Joined: Oct 09, 2012
Posts: 13
Hi everyone,

I've got a problem with my code, which consists of 2 classes.

The error I'm being given at compile is <identifier> expected at the line:

37. poly1.evaluate();

Here is the rest of the code for classes Polynominal and Test:



Hope somebody can help me with this one!
Thanks a lot.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

For future reference, when posting code, please UseCodeTags(⇐click) so it will be readable. I've added them to your original post for you.

As for your problem, you can't call a method out in the middle of nowhere in a class definition. That has to be as part of another method body (such as in main()), or in a constructor or initializer block or a combination variable declaration/initialization statement.
Jack Fletcher
Greenhorn

Joined: Oct 09, 2012
Posts: 13
Great! That error is gone but now there's another one! Any idea what's happened here?



Gives the error:
java.lang.IndexOutOfBoundsException:
Index: 7, Size: 5 (in java.util.ArrayList)

When the Test class is run.
Thanks.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jack Fletcher wrote:


Gives the error:
java.lang.IndexOutOfBoundsException:
Index: 7, Size: 5 (in java.util.ArrayList)


Everything you need to know is right there. You're trying to get the 8th element (at index 7) of a List that has only 5 elements (at indices 0..4). So either you don't have as many things in the list as you thought you did, or you're trying to get a different element than you thought you were trying to get.
Jack Fletcher
Greenhorn

Joined: Oct 09, 2012
Posts: 13
Jeff Verdegan wrote:
Jack Fletcher wrote:


Gives the error:
java.lang.IndexOutOfBoundsException:
Index: 7, Size: 5 (in java.util.ArrayList)


Everything you need to know is right there. You're trying to get the 8th element (at index 7) of a List that has only 5 elements (at indices 0..4). So either you don't have as many things in the list as you thought you did, or you're trying to get a different element than you thought you were trying to get.


I see. So the 'int v' variable is the index position which I want the for loop to use in iterating through the coefficient object. I don't understand why it's iterating further than the size (5) of the arrayList?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jack Fletcher wrote:

I see. So the 'int v' variable is the index position which I want the for loop to use in iterating through the coefficient object. I don't understand why it's iterating further than the size (5) of the arrayList?


Nobody said anything about iterating. If you read the docs for get(), you'll see that get(N) gets the Nth element from the List. In the case of an ArrayList, it does that by direct, indexed access into an array. In the case of a LinkedList, it iterates linearly. In the case of a skip list, it iterates using an approach that should be faster than a simple linear count. But we don't need to know or care about any of that. That's just an implementation detail. All we care about is that get(7) gets the 8th element.

As for why it's getting something larger than the 5th element, it's because you're telling it to. If you can't see where your code is doing that, you should add some print statements that show you what's going on at each step, so you can see where and how v is becoming 7. For instance:



With any luck, that will make your logic error blindingly obvious, and you'll smack your forehead and go, "DUH!" If not, post again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running method on object problem