Я пытаюсь создать программу, которая берет вход пользователя и вводит его в файл. Я хочу, чтобы одна часть установила все и метод, чтобы фактически регистрировать все. Я столкнулся с проблемой, когда пытаюсь использовать файл FileWriter в качестве параметра. Это дает мне такие ошибки, как «FileWriter не может быть разрешен переменной». Как это исправить? И есть ли лучший способ использования FileWriter и метода ListTasks?Использование переменной FileWriter в качестве параметра для метода
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();
}
}
Спасибо! Теперь это начинает иметь смысл! –