Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 24th, 2003, 11:21 AM
suzanne15 suzanne15 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 suzanne15 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old March 24th, 2003, 12:32 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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.

Reply With Quote
  #3  
Old March 24th, 2003, 12:59 PM
suzanne15 suzanne15 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 suzanne15 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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");
}
}
}
}}

}
}

Reply With Quote
  #4  
Old March 24th, 2003, 01:11 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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:
so i want to print out a final string

<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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > searching thro' files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway