Solved

size() and get() problems in while loop

Posted on 2004-10-27
205 Views
Last Modified: 2012-05-05
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
0
Question by:bnblazer
    8 Comments
     
    LVL 2

    Expert Comment

    by:fredwangus
    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
     
    LVL 1

    Author Comment

    by:bnblazer
    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
     
    LVL 1

    Author Comment

    by:bnblazer
    I forgot to include the import in the code above.

    import java.util.ArrayList;
    0
     
    LVL 92

    Accepted Solution

    by:

          for (int i = 0; i < thisList.GetGuessList().size(); i++) {
                 System.out.println("Guess # " + i + "=" + thisList.GetGuessList().get(i));
    0
     
    LVL 1

    Author Comment

    by:bnblazer
    That works, and I have awarded the points - thank you.  If possible, could you explain it a bit?

    Thanks,
    Brian
    0
     
    LVL 92

    Expert Comment

    by:objects
    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
     
    LVL 1

    Author Comment

    by:bnblazer
    Thank you.  It is greatly appreciated.

    Brian
    0
     
    LVL 92

    Expert Comment

    by:objects
    no worries :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    Multiple issues with Java 14 33
    what is spring 3 29
    Annotation to do Dependency injection in spring 3 22
    countHi2 challenge 7 18
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now