Good day!

I would like to ask how to enumerate the contents of certain vector and print it in a certain file because this code is having problems:

Enumeration e = v.elements();
...
while(enu.hasMoreElements()){
      out.write(enu.nextElement());
      }

Error:
...: cannot find symbol
symbol: method write(java.lang.Object)
location: class java.io.BufferedWriter

It seems that out.write seems to be invalid or so because when I changed "out.write" into "System.out.println" to check if upper/lower cases of the functions are correct, the compiler did not give any error.

Thanks for the help. :)

Recommended Answers

All 6 Replies

Did you read the API doc for the BufferedWriter class's write() method?
What data types does write() take for its arguments?
What data type is returned by the nextElement() method?

Can you convert nextElement() returned value to one that is acceptable to write()?

Your Vector does not seem to have any type associated with it, so it's assumed to contain Objects, so enu.nextElement() is seen as returning an Object.
System.out.println is a bit cunning in that it secretly calls the toString() method on anything you try to print, so your enu.nextElement() gets converted to a String, and printed.
However, java.io.BufferedWriter's write method doesn't do that, it needs a proper String (or equivalent - see the API doc for details), and doesn't know how to write unspecified Objects. Hence the error message.
Without knowing how your Vector is declared and populated it's hard to give a "best" answer. Can you supply that info?

@NormR1
To be honest, the API docs is vague to me. The parameters still confuse me even though they are explained well. I am very sorry for that.

@JamesCherrill
I did not know that System.out.println can do that. Thanks for that additional information. :)
As for the info for Vector:

Vector<Dish> dishes = new Vector<Dish>;

where Dish is:

public class Dish{
	private String variant;
	private int minutes;
	Dish next;
	
	Dish(){
		this.variant = null;
		this.minutes = 0;
	}
	
	Dish(String variant){
		this.variant = variant;
		this.minutes = 0;	
	}
	
	public String toString(){
		return variant;
	}

}

So Dish's are objects that have a meaningful toString method. In that case you can simply use that method to convert your enumerated Dish's into Strings that can be written by java.io.BufferedWriter's write method.

ps Since Java 1.5 there's an easier and safer way to iterate through a Vector (or any other kind of Collection) - called the for-each loop. It looks like this example:

Vector<String> v = ...
for (String s : v) { // "for each String s in the Collection v..."
   System.out.println(s);  // or whatever
}

pps Vectors are an old class that have been superceded by ArrayLists for most purposes

ppps Please keep trying with the API doc. Being able to search and use it is just as essential as being able to write a for loop.

Thank you very much for your help. It works now. :)
And I still try to understand the docs. ^^;;

I still try to understand the docs

As James said, being able to read the API docs is very important. I read them dozens of times a day.

If you can post your problems with using the docs, maybe we can help.

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.