Solved

String with String not match

Posted on 2004-04-17
6
291 Views
Last Modified: 2010-03-31
Hi,

I try to find text from text file that refer to another text file.
Why when I use hard code it working but if I read text file and compare with another text file it not working.
My program is as follows:

String AllLine = null;
String ThirdPartyList;
String FindTP;
ThirdPartyList = ("D:/ThirdPartyTest.txt");
FileReader FRTP = new FileReader(ThirdPartyList);
BufferedReader BRTP = new BufferedReader(FRTP);
inFileName = ("D:/SourceMain.txt");
FileReader fileReader1 = new FileReader(inFileName);
BufferedReader br1 = new BufferedReader(fileReader1);
int check1 = 0;

while ((Keyword = BRTP.readLine()) != null)
{
  Keyword = Keyword.substring(Keyword.indexOf("123"),  
                                              Keyword.length());
  Keyword = Keyword.toLowerCase();
  while ((AllLine = br1.readLine()) != null)
  {
    AllLine = AllLine.toLowerCase();
    int index = AllLine.indexOf(Keyword);//here is a problem
    if (index != -1)//text found
    {
       check1 = 1;
       break; //exit while loop when found
                              }
  }//end while
  if (check1 == 1) break;
}//end while
BRTP.close();
br1.close();


if (check1 == 0) {
  JOptionPane.showMessageDialog(StartFrame, "Text Not Found");
}else{
  JOptionPane.showMessageDialog(StartFrame, "Text Found");
}//end else if

Thank you
sara
0
Comment
Question by:sarauwa
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849209
>>it not working.

What do you mean by that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849219
Don't forget to initialize Keyword next to AllLine

String AllLine = null;
String Keyword = null;

0
 

Author Comment

by:sarauwa
ID: 10851109
It's not working because message "Text Not Found" appear even the text exist in both text file.
I have put value null for both Keyword and AllLine.

Example data:
1. Data in text file ThirdPartyTest.txt
abc123aaaaa
defghi123bbbbbbbbbbb
jk123cccc
mnopqr123dddddddddd

2. Data in text file In SourceMain.txt
abc def ghi
jklmno123cccc(this data suppose be found cos same with line 3)
abd xyz

For example if I set hard code Keyword="123cccc" text found but it not work when I read from text file ThirdPartyTest.txt then search in SourceMain.txt

Thank you
sara

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 10851150
you only read the second file once for the first  keyword.
you need reopen the second file for every keyword.

while ((Keyword = BRTP.readLine()) != null)
{
  Keyword = Keyword.substring(Keyword.indexOf("123"),  
                                              Keyword.length());
  Keyword = Keyword.toLowerCase();
  FileReader fileReader1 = new FileReader(inFileName);
  BufferedReader br1 = new BufferedReader(fileReader1);
  while ((AllLine = br1.readLine()) != null)
0
 
LVL 92

Expert Comment

by:objects
ID: 10851151
and of course close it at the end of the inner loop :)
0
 
LVL 92

Expert Comment

by:objects
ID: 10856548
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now