I am writing a small little .jar application that asks user for a string and writes it into a .txt file. I am interested in the program keep executing the following instructions (that is, write date string, and write the input string into the text file) when the input string is not exit. My code does not work for that, ask it does not log the first line I input, nor the first exit I type. I tried a number of things, trying a do-while loop, but that didn't work. I wonder what the problem is?
try{
String input = scanner();
while(!input.equals("exit")){
String fileLocation = "/Users/loop/Dropbox/goodTrance.txt";
FileWriter writer = new FileWriter(fileLocation,true);
writer.append(returnDate()+": ");
writer.append(input + "\n");
writer.flush();
writer.close();
input = scanner();
}
}catch(Exception e){
e.printStackTrace();
}
EDIT: scanner() is a static method that returns a scanner string. returnDate() returns today's date.
public static String returnDate(){
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
return dateFormat.format(date).toString();
}
public static String scanner(){
Scanner input = new Scanner(System.in);
String writeSomething = input.nextLine();
return writeSomething;
}
scanner()method.