Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I don't know why this error using get() method from the ArrayList class

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I don't know why this error using get() method from the ArrayList class

    Hi Every body!

    I'm trying to store (momently) each element of a ArrayList in a Programmer object variable to use one of its method, but I'm getting and and error and a warning I don't know neither why nor how to fix it


    public double getBonusPay() {
            //bonus pay = base bonus * (1 - 0.01 * number of programmers that have overtime pay)
            double totalbonus = 0.00;
            int i;        
            int numProgrammers = 0;
            Programmer proWithoverp;
            for(i = 0; i <= this.juniors.size(); i++){
                proWithoverp = this.juniors.get(i); // <<============here is where I get the error, but I don't know why
                if(proWithoverp.getOvertimePay() > 0){
                    numProgrammers++;
                }
                if(numProgrammers <= 100){
                    totalbonus = getBaseBonus() * (1 - 0.01 * numProgrammers);
                }
            }
     
            return totalbonus;
        }



    in the constructor I declared juniors as:

    this.juniors = new ArrayList();



    this is the error and the warning
    javac -Xlint Test.java
    ./jac444b/a1/Manager.java:24: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
    this.juniors.add(pgr);
    ^
    ./jac444b/a1/Manager.java:56: incompatible types
    found : java.lang.Object
    required: jac444b.a1.Programmer
    proWithoverp = this.juniors.get(i);
    ^
    1 error
    1 warning


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Junior has to be declared as

    ArrayList<Programmers> juniors = new ArrayList();

    or you can cast what you get from the Array

    proWithoverp = (Programmers)this.juniors.get(i);
    Last edited by felimblack; February 11th, 2012 at 02:36 PM.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't know why this error using get() method from the ArrayList class

    hi there!

    How do i do that??? because If I type what you wrote I get a syntax error

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: I don't know why this error using get() method from the ArrayList class

    javac -Xlint Test.java

    Why are you running it with that the Xlint argument? I'm asking this not because its bad, but because you don't understand what it does.
    -Xlint argument enforces stricter checks on your code, throwing warning messages at you in such events as using deprecated methods and unchecked raw objects.

    Felimblack's code isn't running for you either because it still triggers the Xlint command.
    ArrayList<Programmers> juniors = new ArrayList<Programmers>(); would be the correct syntax, or if you're using Java 7, ArrayList<Programmers> juniors = new ArrayList<>(); works just the same.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Location
    Toronto
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't know why this error using get() method from the ArrayList class

    Hi! thanks for answering!


    I'm SO sorry!!!!!!! my bad!!! I wrote ProgrammerS instead of Programmer now the code works fine without any error!! thanks so much!!! both of you
    Last edited by juanp_1982; February 11th, 2012 at 03:21 PM.

Similar Threads

  1. Using the ArrayList Class
    By m2msucks in forum Collections and Generics
    Replies: 9
    Last Post: November 21st, 2011, 02:22 PM
  2. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM
  3. Replies: 2
    Last Post: February 28th, 2011, 10:51 AM
  4. How to use this 2d ArrayList class?
    By J05HYYY in forum Object Oriented Programming
    Replies: 14
    Last Post: January 19th, 2011, 01:48 PM
  5. Converting a method from ArrayList so it is capable with an Array
    By BlueJ1 in forum Collections and Generics
    Replies: 2
    Last Post: July 8th, 2009, 05:22 PM