-2

I am using multidimensional vector. Whenever I wanna write the elements in a file, write() gets error saying "no suitable method found for write(object)". Can you tell me how should I convert Integer object to int so that i can put that in file. Here is the code.

Vector<Vector> vectorA= new Vector<Vector>(1);
Vector<Integer> vectorB= new Vector<Integer>(1);

vectorB.add(1);
vectorA.add(v1);

File f=new File("A1-out1.txt");
f.createNewFile();
FileWriter writer=new FileWriter(f);
writer.write(vectorA.get(0).get(0));
  • Have you first looked this up in the [FileWriter Java API](http://docs.oracle.com/javase/8/docs/api/java/io/FileWriter.html)? This should be answerable with a quick view of this important document, no? – Hovercraft Full Of Eels Mar 01 '17 at 22:52
  • I have seen, but I am trying to put my value in that vector. How to do that? How i should convert Integer object to int? – k132322 Aftab Hussain Mar 01 '17 at 22:58
  • A Writer writes String / char data. If you want to write an int, you need to either convert it to a String or not use a Writer and instead use a more general data output stream. – Hovercraft Full Of Eels Mar 01 '17 at 23:01

1 Answers1

-1

There are no write methods on FileWriter that take an Object. There are methods that take String, characters represented as integers, and a couple of others.

Here are the javadocs of interest: FileWriter, OutputStreamWriter, and Writer.

I think the following code is more of what you're looking to do... Hopefully.

public static void main(String[] args) throws Exception {

    Vector<Vector<Integer>> vectorA = new Vector<>();
    Vector<Integer> vectorB = new Vector<>();

    vectorB.add(1);
    vectorA.add(vectorB);

    File file = new File("A1-out1.txt");
    file.createNewFile();

    FileWriter fileWriter = new FileWriter(file);

    fileWriter.write(vectorA.get(0).get(0).toString());
    fileWriter.flush();
    fileWriter.close();
}
hooknc
  • 4,854
  • 5
  • 31
  • 60
  • 1
    `writer.write(vectorA.get(0).get(0).intValue());` won't this write the int as if it were a char with that int value? I doubt that the OP really wants his data written to file this way. – Hovercraft Full Of Eels Mar 01 '17 at 23:12
  • 1
    So to make it concrete, his int data vector holds a value of 65 as one of its many values, and you're suggesting that he write an `A` char to his file? – Hovercraft Full Of Eels Mar 01 '17 at 23:27
  • Ah, I see what you're saying now. That is what I get for not reading the docs as closely as I should have... Updated my answer. – hooknc Mar 03 '17 at 16:46