Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting a ./Main.java:5: error: cannot find symbol BufferedWriter wrt = BufferedWriter(text);

the code I have written:

import java.io.*;
class Main {
public static void main(String[] args) {
    Writer text = new FileWriter("test.txt");
    BufferedWriter wrt = BufferedWriter(text);
    for (int i = 0; i < 10; i++){
      wrt.write(i); 
      wrt.newLine();
    }
 wrt.flush();
 wrt.close();
 }

}

I’m writing a simple method to create a file and write numbers on each line. I’m utilizing a filewriter to create the actual file, and a buffered writer to actually put in the numbers. When I run my code, I recieve this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  ./Main.java:5: error: cannot find symbol
        BufferedWriter wrt = BufferedWriter(text);
                             ^
  symbol:   method BufferedWriter(Writer)
  location: class Main
1 error

According to the api, Writer can be a parameter for the BufferedWriter, so that shouldn’t be the issue. I spelled everything right as well. API BufferedWriter parameters

>Solution :

You need to use new keyword to create instance of BufferedWriter. please see below working code.

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        Writer text = new FileWriter("test.txt");
        BufferedWriter wrt = new BufferedWriter(text);
        for (int i = 0; i < 10; i++) {
            wrt.write(i);
            wrt.newLine();

        }
        wrt.flush();
        wrt.close();
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading