Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How can I read txt files in java program??

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I read txt files in java program??

    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


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: How can I read txt files in java program??

    What I do is something like this. Of course, there are many ways to do the same thing
    BufferedReader in = new BufferedReader(new FileReader("filename.txt", true));
    String line;
    while ((line = in.readLine()) != null) {
         //Do what you want with each line
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I read txt files in java program??

    it said imported 0 shows
    What variable prints out the value of 0?
    Where is that variable given a value? Add a println statement after every place the value of that variable changes to see if it is getting the correct value.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I read txt files in java program??

    When I press 8 after I run the program, it should sow me how many TV shows are there since
    System.out.println("Import "+listsize+" shows");

    However, it said 0....

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I read txt files in java program??

    Where should I put the code??

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I read txt files in java program??

    Where do you assign a value to listsize? Add the following statement immediately after every statement in the code that changes the value of listsize:
    System.out.println("listsize="+listsize);

    The value would be changed by an assignment statement where listsize is to the left of the = operator.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Using Scanner to read in text files
    By PeskyToaster in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 16th, 2012, 04:57 PM
  2. java program check for files
    By Creeper in forum Java Theory & Questions
    Replies: 2
    Last Post: February 29th, 2012, 02:38 PM
  3. [SOLVED] Compile a set of java files for a sudoku program
    By kanishk.dudeja in forum Java Theory & Questions
    Replies: 7
    Last Post: June 16th, 2011, 09:54 AM
  4. could not open `C:\Program Files\Java\jre6\lib\amd64\jvm.cfg
    By miaaa00 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 29th, 2011, 12:45 AM
  5. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 12:43 PM