size() and get() problems in while loop

I have a method that I am working on (not a class assignment), and my relative newbie status is getting in the way of figuring out what is going wrong.  Here is the code snippet:

private void startPlaying() {
       while (!dotComsList.isEmpty()) {
           GuessList thisList = new GuessList();
           String userGuess = helper.getUserInput("Enter a guess:");
           
           if (userGuess.equals("list")) {
               System.out.println("Retrieving your list of guesses:");
               for (int i = 0; i < thisList.size(); i++) {
                   System.out.println("Guess # " + i + "=" + thisList.get(i));
               }
           } else {
               checkUserGuess(userGuess);
               thisList.storeGuess(userGuess);
           }
       }
       finishGame();
    }

My problem is with the size() and get() in the for loop.  I keep getting these errors:
The method size() is undefined for the type GuessList
      The method get(int) is undefined for the type GuessList

I am importing java.util, and these methods work elsewhere in the class, but I can't figure out why they wont work here, and of course what to do about it.

Any help is greatly appreciated.

Thanks,
Brian
LVL 1
bnblazerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
objectsConnect With a Mentor Commented:

      for (int i = 0; i < thisList.GetGuessList().size(); i++) {
             System.out.println("Guess # " + i + "=" + thisList.GetGuessList().get(i));
0
 
fredwangusCommented:
Did your class GuessList implemented interface List or sub classes of List?
Guess u need to do so if u want to use size and get functions.
0
 
bnblazerAuthor Commented:
GuessList uses ArrayList.  Here is the code:

public class GuessList {
    private ArrayList guesses = new ArrayList();
   
    public void storeGuess(String userGuess) {

        if (!guesses.contains(userGuess)) {
           guesses.add(userGuess);
        }
    }

    /**
     * @return Returns the guesses.
     */
    public ArrayList GetGuessList() {
        return guesses;
    }
    /**
     * @param guesses The guesses to set.
     */
    public void setGuesses(ArrayList guesses) {
        this.guesses = guesses;
    }
}
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
bnblazerAuthor Commented:
I forgot to include the import in the code above.

import java.util.ArrayList;
0
 
bnblazerAuthor Commented:
That works, and I have awarded the points - thank you.  If possible, could you explain it a bit?

Thanks,
Brian
0
 
objectsCommented:
size() and get() are methods of the ArrayList, not methods of your GuessList.
So you need to make the size() and get() calls on the the ArrayList which is accessible via the GetGuessList() method of GuessList.
0
 
bnblazerAuthor Commented:
Thank you.  It is greatly appreciated.

Brian
0
 
objectsCommented:
no worries :)
0
All Courses

From novice to tech pro — start learning today.