My code:
import java.io.*; public class TV { public static void main(String[] args) throws IOException { String fileInput=("TV.txt"); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); if (args.length==1) fileInput=new String(args[0]);{ //If TV.txt doesn't exist, create it! PrintWriter out=new PrintWriter(new FileWriter("TV.txt")); out.println(0); out.close(); } int menu,listsize=0; String[] name=new String[500]; int[] day=new int[500]; String[] time=new String[500]; do { System.out.println("1. Add a TV Show\n2. Modify TV Show\n3. Delete TV Show\n4. Sort TV Shows\n5. Show all TV Shows\n6. Exit\n7. Save TV Shows\n8. Load TV Shows\nWhat is your choice?"); menu=Integer.valueOf(in.readLine()).intValue(); if (menu==1) { System.out.print("Enter a show name: "); //read the data name[listsize]=in.readLine(); System.out.print("Which day? (0 for Sunday, 1 for Monday, etc.) "); day[listsize]=Integer.valueOf(in.readLine()).intValue(); if (day[listsize]==7) day[listsize]=0; //auto correct System.out.print("Enter the show time (24hr format) : "); time[listsize++]=in.readLine(); } else if (menu==2) { System.out.print("Enter the show name: "); String str=in.readLine(); int x; for (x=0;x<listsize;x++) if (str.equals(name[x])) break; if (x==listsize) { System.out.println("Not found"); continue; } System.out.print("Enter the new name: "); name[x]=in.readLine(); System.out.print("Which day? (0 for Sunday, 1 for Monday, etc.) "); day[x]=Integer.valueOf(in.readLine()).intValue(); if (day[x]==7) day[x]=0; //auto correct System.out.print("Enter the show time (24hr format) : "); time[x]=in.readLine(); System.out.print("x="+x); } else if (menu==3) { System.out.print("Enter the show name: "); String str=in.readLine(); int x; for (x=0;x<listsize;x++) if (str.equals(name[x])) break; if (x==listsize) { System.out.println("Not found"); continue; } for (int y=x;y<listsize;y++) { name[y]=name[y+1]; day[y]=day[y+1]; time[y]=time[y+1]; } System.out.println("Removed"); listsize--; } else if (menu==4) { System.out.println("Which way do you want to sort?\n1. name\n2. day\n3. time"); int way=Integer.valueOf(in.readLine()).intValue(); if (way==1) { for (int x=0;x<listsize;x++) { //sort int smallest=x; for (int y=x;y<listsize;y++) if (name[y].compareTo(name[smallest])<0) smallest=y; String tmp=name[x]; //Swap name[x]=name[smallest]; name[smallest]=tmp; tmp=time[x]; time[x]=time[smallest]; time[smallest]=tmp; int t=day[x]; day[x]=day[smallest]; day[smallest]=t; } } else if (way==2) { for (int x=0;x<listsize;x++) { //sort int smallest=x; for (int y=x;y<listsize;y++) if ((day[y]<day[smallest]) || ((day[y]==day[smallest])&&(name[y].compareTo(name[smallest])<0))) smallest=y; String tmp=name[x]; //Swap name[x]=name[smallest]; name[smallest]=tmp; tmp=time[x]; time[x]=time[smallest]; time[smallest]=tmp; int t=day[x]; day[x]=day[smallest]; day[smallest]=t; } } else if (way==3) { for (int x=0;x<listsize;x++) { //sort int smallest=x; for (int y=x;y<listsize;y++) if ((time[y].compareTo(time[smallest])<0) || ((time[y].compareTo(time[smallest])==0)&&(name[y].compareTo(name[smallest])<0))) smallest=y; String tmp=name[x]; //Swap name[x]=name[smallest]; name[smallest]=tmp; tmp=time[x]; time[x]=time[smallest]; time[smallest]=tmp; int t=day[x]; day[x]=day[smallest]; day[smallest]=t; } } System.out.println("Sorted"); } else if (menu==5) { int[] showPerDay=new int[7]; System.out.println("Name, Day, Time"); for (int x=0;x<listsize;x++) { System.out.print(name[x]+", "); if (day[x]==0) System.out.print("Sun"); else if (day[x]==1) System.out.print("Mon"); else if (day[x]==2) System.out.print("Tue"); else if (day[x]==3) System.out.print("Wed"); else if (day[x]==4) System.out.print("Thu"); else if (day[x]==5) System.out.print("Fri"); else if (day[x]==6) System.out.print("Sat"); System.out.println(", "+time[x]); showPerDay[day[x]]++; } System.out.println("There are "+showPerDay[0]+" per Sunday, "+showPerDay[1]+" per Monday, "+showPerDay[2]+" per Tuesday, "+showPerDay[3]+" per Wednesday, "+showPerDay[4]+" per Thursday, "+showPerDay[5]+" per Friday, "+showPerDay[6]+" per Saturday"); } else if (menu==7) { PrintWriter out=new PrintWriter(new FileWriter("TV.txt")); out.println(listsize); for (int x=0;x<listsize;x++) { out.println(name[x]); out.println(day[x]); out.println(time[x]); } out.close(); System.out.println("Done"); } else if (menu==8) { BufferedReader inFile= new BufferedReader(new FileReader("TV.txt")); listsize =Integer.valueOf(inFile.readLine()).intValue(); for (int y=0;y<listsize;y++) { name[y]=inFile.readLine(); day[y]=Integer.valueOf(inFile.readLine()).intValue(); time[y]=inFile.readLine(); } System.out.println("Import "+listsize+" shows"); } System.out.println(); } while (menu!=6); } }
When I press "8", it said imported 0 shows. However, there are 7 shows in my TV.txt file. How can the java program read my TV.txt file properly?? Do I need to write anything specific in the txt file??
Here is my txt file:
Cooking Mama, Mon, 08:00 Gossip Boy, Tue, 22:00 Be Home for Dinner, Wed, 20:00 The Big Boy Show, Thurs, 21:30 Crime Scene, Fri, 22:00 Movie of the Week, Sat, 21:00 Cooking with Jim, Sun, 09:00