-1

I am trying to create a program that takes user input and enters it into a file. I want one part to set everything up and a method to actually log everything. I run into an issue when I try to user FileWriter writer as a parameter. It gives me errors such as "FileWriter cannot be resolved to a variable". How do I fix this? And is there a better way of using the FileWriter and the Method ListTasks?

public class TaskCreate 
{
static Scanner taskInput = new Scanner(System.in);
static Scanner userContinue = new Scanner(System.in);
static String decision = "yes";
static int i = 1;

public static void Create() throws IOException 
{   
    try
    {
        File tasklist = new File("tasklist.txt");
        tasklist.createNewFile();
        // creates a FileWriter Object
        if(tasklist.equals(true))
        {
            FileWriter writer = new FileWriter(tasklist);
        }
        else
        {
            FileWriter writer = new FileWriter(tasklist, true);
        }

//***********************************************
         //I get the "FileWriter cannot be resolved to a variable" here.
         //I also get the "Syntax error on token "writer", delete this              //token" here
        ListTasks(FileWriter writer);
//***********************************************
        taskInput.close();
        userContinue.close();
    }
    catch(Exception e)
    {
    System.out.print("You did something wrong");    
    }
}
//****************************************************************
//Why does it work here and not up there?
public static void ListTasks(FileWriter writer) throws IOException
//*********************************************************************
{
    while(decision.equals("yes"))
    {                   
        System.out.print("Please enter a task: ");
        String task = taskInput.nextLine();

            // Writes the content to the file
        writer.write(i + " " + task + System.getProperty( "line.separator" ));

        System.out.print("Would you like to enter another task? yes/no: ");
        decision = userContinue.nextLine();
        System.out.print(System.getProperty( "line.separator" ));

        ++i;
    }
    writer.flush();
    writer.close();
}   
}
Josiah L.
  • 302
  • 1
  • 4
  • 21

1 Answers1

1

First of all, here you create local variable writer inside {} block and it goes out of scope as soon as if has been executed.

     // creates a FileWriter Object
        if(tasklist.equals(true))
        {
            FileWriter writer = new FileWriter(tasklist);
        }
        else
        {
            FileWriter writer = new FileWriter(tasklist, true);
        }

Rewrite it like that:

    FileWriter writer = null;

    if(tasklist.equals(true))
    {
        writer = new FileWriter(tasklist);
    }
    else
    {
        writer = new FileWriter(tasklist, true);
    }

Then, you wrote:

ListTasks(FileWriter writer);

It's not correct, you should write just a variable's name without it's type, that is:

    ListTasks(writer);
olegst
  • 1,209
  • 1
  • 13
  • 33