Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

For loop through a vector not allowing me to use .length?

 
Dave Allen UK
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I am currently writing a Scrabble program and working on the bag that contains all of the tiles. I have set up the tiles using a vector that stores their frequency. I am trying to iterate through the vector using a for loop but for some reason I keep getting the following error,

This happens everytime I try and iterate through the vector which is a few times. My code for the whole things is as follows,

I should mention that this is for my uni coursework and we are not allowed to use calsses from the java.util package, I was wondering if that is the reason it does not work.

Any help is very very much appreciated. Thanks very much.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you define length in MyVector? Is it a method or a field?
 
Dave Allen UK
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I thought that the .length method returned how many elements there were in the vector. I have also tried tilelist.size() but that does not seem to work either. Not sure if that answers your question though, do I need to have a method in the MyVector that adds up how many there are?
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're asking about that MyVector class you posted up there, then you can look at the code yourself and see what methods it has. You can see that it doesn't have any size() method just by looking at it. (There are two methods, but neither of them are called size().) Likewise you can see that it doesn't have a length variable. (It has two variables but neither are called length.)

But wait! This MyVector class might be a subclass of something which has one of those things! But no... it's only a subclass of Object, which doesn't have a size() method or a length variable.

So I don't know where you expected those methods to come from. Is that your class? Then it's up to you to write them. They won't appear by magic just because you try to use them.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You would find it easier to set up the tiles with chars in your for loop. You mightn't believe it, but you can alter it toHave you been told you use Vector rather than the more efficient ArrayList? Do you know how to put the letters, counts and tile values into a Map?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic