hello,

I have called getFruits method from another class (another class is called vegetables). I can't seem to add the names of the fruits in myTable TreeSet in class Test. The error I am getting is "cannot find symbol - variable length". Can anyone help me. Thanks

public class Test
{
 
  private static TreeSet<String> myTable = new TreeSet<String>();  

  public Test()
   {
     super();
     myTable = new TreeSet<String>();
   }


  public static void shoppingList()
  {
  
    vegetables calling = new vegetables();
     
    System.out.println(" " + calling.getFruits()); // used this to test if I able to    
                                                   // call the method from another class
    
    for (int i = 0; i < calling.length; i++) // here is there error occuring
    {
       myTable.add(calling[i]);
    }
  }
}

vegetables class doesn't have a variable called length.
Were you thinking of whatever the getFruits() method returns? Sounds like that may be an array? In which case that's what you should be using instead of calling.

Yes that is what I am trying to do. I don't know how to for loop an array when that array belongs to a method when is called into another class.

Assuming it's a String array, you can create a local variable to refer to the array, then use that in your loop, eg

String[] fruits = calling.getFruits();
for (int i = 0; i < fruits.length; i++) {
myTable.add(fruits)
}

Thanks James it is String array but unfortunately I have the following error message "incompatible types - found java.util.Set<java.lang.String>but expected java.lang.String[]" Why is that?

You need to declare the local variable to be whatever type the method returns - I haven't seen the code, so I just had to guess.

In the instructions it states...
"The vegetables class has a public class method called getFruits() which takes
no arguments and returns a set of strings. The set it returns is a small set of words that you can use to populate myTable." There is no source code for vegetables class, only complier version.

Write a public instance method called shoppingList() that takes no arguments and
returns no value. The method should iterate over the set returned by getFruits(),
adding each word to myTable.

Set (as in "Set of Strings") is a Java class that can hold a number of Objects and allows you to iterate through them (though not in the same way you loop thru an array). You'll find the details in the Java API doc.

the set returned by getFruits()

It'd help if you defined what type of data (the java class) is returned by the getFuits() method.

Norm - the clue is a few posts back in the error message. Its java.util.Set<java.lang.String>
The O/P didn't know that, which was making his life hard.
I've got to go now - can you take this one over for me?
Thanks
James

honestly there isn't much but could this be helpful
vegetables.getFruits()
returned:
Set<String> getFruits()
there are four sets, each set naming fruit names.

Perhaps using Maps. Again I really need to see some examples.

Yes, that is helpful. getFruits() returns a Set<String>

You need to change your code so that the receiving field (fruits) is a Set<String> vs a String array.

Read the API doc for the Set interface to see what methods you can use to get at the contents of the Set object.

How do you need to access the data in the Set?

the answer is addAll. This is solved thanks to both of you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.