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();
}
}