IN my java code, i wrote functions in order to write data into a txt file and delete from it. the code just stopped running for some reason. I think it is in this file of code. Please send any tips.This is my code. if nothing is wrong with this file, please tell me.
import java.util.*;
import java.util.regex.Pattern;
import java.time.*;
import java.io.*;
import java.text.*;
import java.time.temporal.ChronoUnit;
import java.nio.file.Files;
import java.nio.file.Paths;
public class input{
public LocalDate date1;
public input (LocalDate date1){
this.date1=date1;
}
public void getSubject(){
try{
long count= Files.lines(Paths.get("savedata.txt")).count();
Scanner scanner= new Scanner(System.in);
System.out.println("When you are done entering homework, enter 'done' at subject");
FileWriter savedata = new FileWriter("savedata.txt", true);
PrintWriter writer = new PrintWriter(savedata);
Homework homework1= new Homework("empty", "2024-08-24");
System.out.println("Enter the subject of the homework");
homework1.subject= scanner.nextLine();
if(homework1.subject.equals("done")){
return;
}
writer.println(homework1.subject);
writer.close();
}catch(Exception c){
}
}
public void getDate() throws ParseException{
try{
long count= Files.lines(Paths.get("savedata.txt")).count();
Scanner scanner= new Scanner(System.in);
Scanner readlines = new Scanner("savedata.txt");
FileWriter savedata = new FileWriter("savedata.txt", true);
PrintWriter writer = new PrintWriter(savedata);
Homework homework1= new Homework("empty","2024-08-24" );
//LocalDate.now()
System.out.println("Enter the due date of the homework: YYYY-MM-DD");
homework1.dueDate= scanner.nextLine();
//LocalDate.parse
while(homework1.dueDate!="done"){
writer.println(homework1.dueDate);
break;
}
while(homework1.dueDate.equals("done")){
File oldfile = new File("savedata.txt");
File newfile = new File("temp.txt");
int line=0;
String currentline;
FileWriter fw= new FileWriter("temp.txt");
BufferedWriter bw =new BufferedWriter(fw);
PrintWriter pw= new PrintWriter(bw);
FileReader fr= new FileReader("savedata.txt");
BufferedReader br = new BufferedReader(fr);
while((currentline=br.readLine())!=null){
line++;
if(count!=line){
pw.println(currentline);
}
}
pw.flush();
pw.close();
br.close();
fw.close();
bw.close();
fr.close();
oldfile.delete();
File dump=new File("savedata.txt");
newfile.renameTo(dump);
break;
}
writer.close();
}catch(Exception c){
}
}
public void getArray(){
try{
long count= Files.lines(Paths.get("savedata.txt")).count();
Scanner scanner= new Scanner(System.in);
FileWriter savedata = new FileWriter("savedata.txt", true);
PrintWriter writer = new PrintWriter(savedata);
Homework homework1= new Homework("empty", "2024-08-24");
System.out.println("Enter parts of homework. Write done when done.");
String finish="";
while(finish.equals("done")){
File oldfile = new File("savedata.txt");
File newfile = new File("temp.txt");
int line=0;
String currentline;
FileWriter fw= new FileWriter("temp.txt");
BufferedWriter bw =new BufferedWriter(fw);
PrintWriter pw= new PrintWriter(bw);
FileReader fr= new FileReader("savedata.txt");
BufferedReader br = new BufferedReader(fr);
while((currentline=br.readLine())!=null){
line++;
if((count!=line)|| (count-1!=line)){
pw.println(currentline);
}
}
pw.flush();
pw.close();
br.close();
fw.close();
bw.close();
fr.close();
oldfile.delete();
File dump=new File("savedata.txt");
newfile.renameTo(dump);
break;
}
while(!finish.equals("done")) {
finish= scanner.nextLine();
if(!finish.equals("done")) {
parts newpart = new parts(finish);
homework1.addPart(newpart);
}
}
System.out.println(homework1.part);
writer.println(homework1.part);
writer.close();
}catch(Exception c){
}
}
}
I was expecting my code to run normally without errors but the code runner just ends.
New contributor
Natalie Ajmeri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.