|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
searching thro' files
hi
I have two seperate file (examples of each below) i want to read thro' each file checking for the same string, so i want to print out a final string (for the example below SH 02 NFC S because there is one in each file) file 1(spot.txt) AU 51 LXC S SH 02 NFC S Y 926 XHJ S SS 02 DEZ S Y 297 AAZ S Y 455 SDA S file2(PAID.txt) SI 02 NMH S Y 264 XML S SP 51 CTE S Y 944 ERT S X 494 DHN S SH 02 NFC S Below is what i already have but not sure what to do when im checking each character *if(spot.charAt(i)==PAID.charAt(j))* import java.io.*; import java.lang.Object.*; class Test { public static void main (String[] args)throws Exception { FileReader fr = new FileReader("spotted.txt"); FileReader FR = new FileReader("paid.txt"); BufferedReader br = new BufferedReader(fr); BufferedReader BR = new BufferedReader(FR); String spot = new String(); String PAID = new String(); while((spot = br.readLine())!=null){ while((PAID= BR.readLine())!=null){ for(int i=0;i<=spot.length();i++){ for(int j=0;j<=PAID.length();j++){ if(spot.charAt(i)==PAID.charAt(j)) { // System.out.println(PAID); } else{;} } } }} } } Thank you in advance for your help suzanne ![]() Last edited by suzanne15 : March 24th, 2003 at 11:32 AM. |
|
#2
|
|||
|
|||
|
Seems like you want to compare two strings, but you found out already that the "=" operator does not work (one good example where java breaks itīs own design
)...Java is a very high level language. It has methods and classes for everything. To compare two strings, use (in your case): Code:
...
if (spot.equals(PAID)) {
...
} else {
...
}
...
![]()
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
|||
|
|||
|
Right, thanx i changed it and put in the extra print statement, it just prints out lots of hello's but it should print out Y 926 XHJ .
If you have any suggestions i be extremely grateful thanx very much import java.io.*; import java.lang.Object.*; class Test { public static void main (String[] args)throws Exception { FileReader fr = new FileReader("f.txt"); FileReader FR = new FileReader("f2.txt"); BufferedReader br = new BufferedReader(fr); BufferedReader BR = new BufferedReader(FR); String spot = new String(); String PAID = new String(); while((spot = br.readLine())!=null){ while((PAID= BR.readLine())!=null){ for(int i=0;i<=spot.length();i++){ for(int j=0;j<=PAID.length();j++){ if(spot.equals(PAID)) { System.out.println(spot); } else{ System.out.println("hello"); } } } }} } } |
|
#4
|
|||
|
|||
|
Why are you still comparing the strings as often as the product of their lengths?
(I want to say: your for-loops are not necessary anymore )Code:
while((spot = br.readLine())!=null){
while((PAID= BR.readLine())!=null){
if (spot.equals(PAID)) {
...
}
}
Quote:
<dumb mode>What do you want this final string to look like?</dumb mode> Sorry for kidding, i think you want to output the strings that are in both files, right? --> The answer: Using your existing code, you will have to rewind the inner stream on each run or it wonīt find any lines starting from the second iteration because it is already at its end and thus the inner while() loop will not execute anymore. Some overhead, yes. IMHO you should read both files into an array (if they donīt contain thousands of lines) and search inside the arrays then to save disk access time. But for files containing only a few lines, go with the answer above. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > searching thro' files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|