Here is my code for a line/file editor: Please help me figure out when I input "E" to exit and save to the textin.txt file, it is not saving anything in textin.txt file. It also is prints "1>" twice when I hit enter before it will move on to "2>" See my output below and the needed output and I need the command "E" to save the output to the textin.txt file and terminate the program. "E" terminates my program, but is not saving to the txt file. import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class FileEdit { public static List lines = new ArrayList(); public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); System.out.print("EDIT: "); String fileName = s.nextLine(); BufferedReader br = new BufferedReader(new FileReader(new File(fileName))); String str=""; int count=0; while((str=br.readLine())!=null) { lines.add(count, str); System.out.println((++count) + "> "+ str); } String text=""; String command = ""; System.out.print((++count)+"> "); command=s.nextLine(); String commandArr[]; int flag; while(!command.equals("E")) { flag=0; commandArr = command.split("\\s"); if(commandArr[0].equals("I")) //Insertion { while(true) { if(commandArr.length==1&&flag!=2) { System.out.print((count)+"> "); text = s.nextLine(); insertLine(text,count-1); } else if(commandArr.length==2&&flag!=2) { System.out.print((commandArr[1])+"> "); count=Integer.parseInt(commandArr[1]); text = s.nextLine(); insertLine(text,Integer.parseInt(commandArr[1])-1); } else insertLine(text,count-1); System.out.print((++count)+"> "); command=s.nextLine(); commandArr = command.split("\\s"); if(commandArr[0].equals("I")||commandArr[0].equals("L")||commandArr[0].equals("D")||comm andArr[0].equals("E")) { flag=1; break; } else { flag=2; text=command; } } } else if(commandArr[0].equals("L")) //Listing { printList(); } else if(commandArr[0].equals("D")) //Deletion { if(commandArr.length==1) { deleteLine(count-1,count-1); } else if(commandArr.length==2) { deleteLine(Integer.parseInt(commandArr[1])-1,Integer.parseInt(commandArr[1])-1); } else if(commandArr.length==3) { deleteLine(Integer.parseInt(commandArr[1])-1,Integer.parseInt(commandArr[2])-1); } count=lines.size(); } if(flag!=1) { System.out.print((count++)+"> "); command = s.nextLine(); } } } public static void insertLine(String text,int count) { if (count >= lines.size()) { for(int i = lines.size(); i <= count; i++) { lines.add(null); } } lines.set(count,text); } public static void deleteLine(int start,int end) { for(int i=start;i<=end;i++) lines.remove(i); } public static void printList() { for(int i=0;i "+lines.get(i)); } } Here is the output I am getting: EDIT: textin.txt 1> The First Line 1> 2> And another line 3> I 3 3> The second line 4> One more line 5> L 1> 2> 3> The second line 4> One more line 5> D 2 3> L 1> 2> The second line 3> One more line 4> E Out.