Austin
Austin

Reputation: 11

Writing text files in java

So I know there are a lot of examples with writing textfiles, but I can't seem to get what is wrong with mine.

Here is the code I have so far

private void saveAddressBook() {
    try {  
        PrintWriter out = new PrintWriter("filename.txt");
        out.println(fullName);
        out.close();
       } catch(ValidationException e) {
              e.printStackTrace();
       }
}

I get an error on out.println(fullName); that says "fullName cannot be resolved to a variable"

Which makes no sense because I use it in other methods and classes. Not to mention it is public.

So what am I doing wrong?

Edit: fullName is declared right at the top of the class. I just didn't include it.

Upvotes: 0

Views: 62

Answers (2)

DripDrop
DripDrop

Reputation: 1002

Your problem is that you have not defined the fullName variable.

Using out.println("FirstName LastName"); will work just fine, but if you want to pass the fullName variable as an agument to saveAddressBook that would look like:

private void saveAddressBook(String fullName) {
    try {  
        PrintWriter out = new PrintWriter("filename.txt");
        out.println(fullName);
        out.close();
       } catch(ValidationException e) {
              e.printStackTrace();
       }
}

Upvotes: 1

Rodrigo Lopez Guerra
Rodrigo Lopez Guerra

Reputation: 754

You didn't declare the variable fullName . Try to declare and assign a value to the variable or give the String as the argument

out.println("my super string");

regards,

Upvotes: 1

Related Questions