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 9 of 9

Thread: FILE READER PROGRAM

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default FILE READER PROGRAM

    This is my program in which m reading from a filr with approx 40000 Strings in which m making a combination of all 40000 strings with other using a while loop but the loop is running only for first entry of the nested loop what is the problem with this code and can any body give me sugessions for such a huge loop??
    package combinator;
     
    import java.io.*;
     
    import Reader.HandsFileReader;
     
    public class FinalCombination{
     
    	HandsFileReader hfr=new HandsFileReader();
    	BufferedReader b,s;
    	String bs1,bs2, ss;
    	final String separator =System.getProperty("line.separator");
    	public void finalRead(){
    		try{
    			FileWriter fr=new FileWriter("F:/java/Project/outputfiles/FinalCombination.txt");
     
    			b=hfr.readSingle();
    			s=hfr.readSamyukta();
     
    				while((bs1=b.readLine())!=null){
    					while((bs2=b.readLine())!=null){
    						String temp=(bs1+","+bs2); 
    						char []c=temp.toCharArray();
    						for(int i=0;i<c.length;i++){
    						fr.append(c[i]);
    					}
    						fr.append(separator);
    				}
    			while((ss=s.readLine())!=null){
    				String temp=(ss+","+ss); 
    				char []c=temp.toCharArray();
    				for(int i=0;i<c.length;i++){
    				fr.append(c[i]);
    				}
    				fr.append(separator);
     
    			}
    			fr.close();
     
    		}
    				}
    		catch(IOException e){
    			e.printStackTrace();
    		}
    		System.out.println("Done");
    	}
    	public static void main(String[] args) {
    		FinalCombination fc=new FinalCombination();
    		fc.finalRead();
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: FILE READER PROGRAM

    Please post in the correct forum. Thread moved to - File I/O & Other I/O Streams

    You need to provide the input file and also give an example of the desired output.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

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

    Default Re: FILE READER PROGRAM

    loop is running only for first entry of the nested loop
    There is more than one nested loop. Please add some comments to the code explaining WHERE the problem is. Which loop executes and how many times and which loop only executes once etc.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FILE READER PROGRAM

    package combinator;

    import java.io.*;

    import Reader.HandsFileReader;

    public class FinalCombination{

    HandsFileReader hfr=new HandsFileReader();
    BufferedReader b1,b2,s;
    String bs1,bs2, ss;
    final String separator =System.getProperty("line.separator");
    public void finalRead(){
    try{
    FileWriter fr=new FileWriter("F:/java/Project/outputfiles/FinalCombination.txt");

    b1=hfr.readSingle();
    b2=hfr.readSingle();
    s=hfr.readSamyukta();

    while((bs2=b1.readLine())!=null){
    while((bs1=b2.readLine())!=null){
    String temp=(bs1+","+bs2);
    char []c=temp.toCharArray();
    for(int i=0;i<c.length;i++){
    fr.append(c[i]);
    }
    fr.append(separator);
    }
    while((ss=s.readLine())!=null){
    String temp=(ss+","+ss);
    char []c=temp.toCharArray();
    for(int i=0;i<c.length;i++){
    fr.append(c[i]);
    }
    fr.append(separator);

    }
    fr.close();

    }
    }
    catch(IOException e){
    e.printStackTrace();
    }
    System.out.println("Done");
    }
    public static void main(String[] args) {
    FinalCombination fc=new FinalCombination();
    fc.finalRead();
    }
    }


    The Underlined second loop is executing only once while thw outer loop executes untill the end of the file
    here is the sample of the input file


    an,0,0,0,0,0,0
    swa,0,0,0,0,0,0
    dol,0,0,0,0,0,0
    pus,0,0,0,0,0,0
    katv,0,0,0,0,0,0
    an,0,0,0,0,90,0
    swa,0,0,0,0,90,0
    dol,0,0,0,0,90,0
    pus,0,0,0,0,90,0
    katv,0,0,0,0,90,0
    an,0,0,0,0,180,0
    swa,0,0,0,0,180,0
    dol,0,0,0,0,180,0
    pus,0,0,0,0,180,0
    katv,0,0,0,0,180,0
    an,0,0,0,90,0,0
    swa,0,0,0,90,0,0

    total 20,000 input both the file and i want their all possible combinations



    and the output i want is like this all possible combination of two files with 20,000 string entries
    an,0,0,0,0,0,0swa,0,0,0,0,0,0
    dol,0,0,0,0,0,0pus,0,0,0,0,0,0
    katv,0,0,0,0,0,0an,0,0,0,0,90,0
    swa,0,0,0,0,90,0dol,0,0,0,0,90,0
    pus,0,0,0,0,90,0katv,0,0,0,0,90,0
    an,0,0,0,0,180,0swa,0,0,0,0,180,0
    dol,0,0,0,0,180,0pus,0,0,0,0,180,0
    katv,0,0,0,0,180,0an,0,0,0,90,0,0

    please suggest if dere is any other possible way to get such a huge combination

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: FILE READER PROGRAM

    while((bs1=b.readLine())!=null){
        while((bs2=b.readLine())!=null){
    The outer loop will read the first line in the file. The inner loop will then read all the other lines in the file. Now there are no more lines to read so both loops exit. I take it this is not what you want, especially since the outer loop appears to be pointless. In that case you need to rethink your design.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FILE READER PROGRAM

    i want outer loop to take one entry from the file1 den combine it with all entries in file 2 again take second entry from file 1 and combine it with all entry in file 2 and so on

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FILE READER PROGRAM

    while((bs1=b1.readLine())!=null){
    while((bs2=b2.readLine())!=null){

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: FILE READER PROGRAM

    Where do you initialise the BufferedReaders?
    Improving the world one idiot at a time!

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: FILE READER PROGRAM

    in another program in a package Reader.HeadFileReader.java with a method which has a return type BufferedReader object
    u can see the buffered reader references are assingned to hfr.readSingle() hfr=HandFileReader object and readSingle() is a method with return type BufferedReader

Similar Threads

  1. Java IO File Reader Question???
    By liamShannon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 12th, 2011, 03:37 PM
  2. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 10:48 AM
  3. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 08:14 PM
  4. greetings and a file reader problem
    By chileshe in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 6th, 2009, 03:45 AM
  5. Code to read a character in the file
    By Truffy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 19th, 2009, 06:11 PM

Tags for this Thread