• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Running method on object problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic