-1

I am trying to instantiate a object with BufferedWriter and it wont work. The problem happens when I use the write function. Why won't it let me write to the file? The error is cannot find symbol. Please help me. I know someone knows. Why wont it not find the symbol when this is a bufferedWriter method?

package ex5_abcd;

import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;

public class EX5_ABCD {

    public static void main(String[] args) {
        boolean go = true;
        String firstN, lastN;
        String lineWritten = "";
        int IdNum;
        Scanner input = new Scanner(System.in);
        Path file = Paths.get("C:\\Java\\empList.txt");
        try {
            OutputStream output = new BufferedOutputStream(Files.newOutputStream(file, CREATE));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
            while (go) {
                System.out.println("Please enter Employee's First Name");
                firstN = input.nextLine();
                System.out.println("Please enter Employee's Last Name");
                lastN = input.nextLine();
                System.out.println("Please enter " + firstN + " " + lastN);
                IdNum = input.nextInt();
                lineWritten = IdNum + " " + firstN + " " + lastN;
                int lineLength = lineWritten.length();
                char [] testChar  =  new char[1];
            testChar [0] = 'a';
            writer = write(testChar, 0, lineLength); // Why write error
                writer.flush();
                writer.newLine();

            }
            writer.close();
        } catch (Exception e) {
            System.out.println("Error Msg:" + e);
        }
    }
}

1 Answers1

0
writer = write(lineWritten, 0, lineLength);

should be rewritten to

writer.write(lineWritten, 0, lineLength); 

since writer is not a reference to an object, you should call the method of the writer object, not set writer to another value

Recap:

(=) sets a value.

Also, since you never set the value of go as false in your loop, you will keep on looping there... forever... and ever.... I suggest to not let that happen

EDToaster
  • 3,160
  • 3
  • 16
  • 25