I'm trying to compare lines in two different files. The basic idea is I want to take line 1 of file 1 and compare it to all the lines of file 2 and print the results into a third file, then do the next line of file 1 and repeat the above steps. The problem I'm having right now is that the program reads line 1 of file 1, compares it to all the lines in file 2 (like i want it to), then it just reads the rest of the lines in file 1 without going into file 2 to compare again. For reference, pppReader is for "file 1" and seReader is for "file 2". Here is my code:

BufferedReader pppReader = new BufferedReader(new FileReader("A261r_34PPPgalaxy.cat"));
BufferedReader seReader = new BufferedReader(new FileReader("A261r_34_CLASS.cat"));
PrintWriter writer   = new PrintWriter(new                    FileWriter("A261r_34PPPgalaxy.plot"));
		    
		    String pppLine = null;
		    String seLine = null;
		    while ( (pppLine=pppReader.readLine()) != null)
		    {
		    	String[] pppColumns = pppLine.split(" ");
		    	
		    	while ((seLine=seReader.readLine()) != null)
		    	{
			    	String[] seColumns = seLine.split("\\s+");
			    	
			    	float xCompare = Math.abs(Float.parseFloat(pppColumns[0]) - Float.parseFloat(seColumns[26]));
			    	float yCompare = Math.abs(Float.parseFloat(pppColumns[1]) - Float.parseFloat(seColumns[27]));
			    	
			    	if (xCompare <= 4000 && yCompare <= 4000)
			    	{
			    		float autoVsAper = 
						  	  Float.parseFloat(seColumns[19]) - Float.parseFloat(seColumns[9]);
						
			    		writer.println(autoVsAper + "\t" + seColumns[19]);
			    	}
		    	}
		    	
		    }
		    
		    writer.close();
		    pppReader.close();
		    seReader.close();

Some System.out.println's showed me that it only goes into the second while loop the first time through, then never again. How do I fix it so it goes into file 2 everytime a line of file 1 is read

Recommended Answers

All 3 Replies

You would probably need to reset() the stream to get that to work. After it's read all the way through the file, seLine=seReader.readLine() is going to remain null - it's done reading.

Unless your files are so large as to cause memory problems, you'd get a lot better performance reading those comparison values into a collection from the file and using that collection for your line item comparisons.

Could you show me the proper way to implement the reset(). I tried it right after the second while was finished but it didn't work. Or I don't know how to mark() it, either is possible.

As for file size, the bigger files are roughly 700kb, with about 1400-1500 lines and about 45 columns each line.

If you're only working with 1400-1500 lines I would read them into another structure for the comparisons or you could use random access files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.