Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

null pointer exception in string comparison

saran185saran185 Posts: 23Member
hi..
I want to compare the contents of text files. I have to display the mis matched strings and the number of mis matched words. Here i have my code below. but i get null pointer exception after the display of the mismatched words. the number of mis matched words is not getting displayed. can anyone fix the bug.
thanks..
[code]
public static void main(String[] args) {
// TODO code application logic here
String [] str1 = new String[800];
String [] str2 = new String[800];
String [] str3 = new String[800];
int cnt3=0;
File f1=new File("d:/temp/test1.txt");
File f2=new File("d:/temp/test2.txt");
if((!f1.exists())&&(!f2.exists()))
{System.out.println("file not found");
}

else
{
try
{
// FileInputStream in1 = new FileInputStream(f1);

FileReader file1 = new FileReader(f1);
StreamTokenizer streamTokenizer1 = new StreamTokenizer(file1);
FileReader file2 = new FileReader(f2);
StreamTokenizer streamTokenizer2 = new StreamTokenizer(file2);

int c1=0;
int i=0;
int numberOfTokensGenerated1 = 0;
while(i != StreamTokenizer.TT_EOF){
str1[c1]=streamTokenizer1.toString();
c1++;
i = streamTokenizer1.nextToken();
numberOfTokensGenerated1++;
}

//System.out.println("total mispelled words " +c1);
int c2=0;
int j=0;
int numberOfTokensGenerated2 = 0;
while(j != StreamTokenizer.TT_EOF){
str2[c2]=streamTokenizer2.toString();
String w = streamTokenizer2.toString();
// System.out.println(w);
c2++;
j = streamTokenizer2.nextToken();
numberOfTokensGenerated2++;
}

int q=0;
int k;
if(c1==c2)
{
for( k=0;k<=c1;k++)
{
if(str1[k].compareTo(str2[k])==0)
{ System.out.println("this string is eq" +str1[k]);
}
else
{ str3[q]=str2[k];
q++;

}
}

System.out.println("
Total Mispelled words" +q);
System.out.println("
Mispelled words");

for(i=0;i<q;i++)
{
System.out.println(str3[i]);

}

}

}catch(IOException e)
{ e.printStackTrace();

}



}
}


[/code]

Comments

  • karan8490karan8490 Posts: 9Member
    can we use regex pattern & matcher in this program? pass the f1 file's string in pattern & matcher with f2...
  • karan8490karan8490 Posts: 9Member
    //[color=Blue][b]Use This->[/b][/color]



    import java.io.*;
    class gjgh
    {
    public static void main(String[] args)
    {
    // TODO code application logic here
    String [] str1 = new String[20000];
    String [] str2 = new String[20000];
    String [] str3 = new String[20000];
    int cnt3=0;
    File f1=new File("d:/lyerics/sd.txt");
    File f2=new File("d:/lyerics/jk.txt");
    if((!f1.exists())&&(!f2.exists()))
    {
    System.out.println("file not found");
    }
    else
    {
    try
    {
    FileInputStream file1 = new FileInputStream(f1);

    //FileReader file1 = new FileReader(f1);
    StreamTokenizer streamTokenizer1 = new StreamTokenizer(file1);
    // FileReader file2 = new FileReader(f2);
    FileInputStream file2 = new FileInputStream(f2);
    StreamTokenizer streamTokenizer2 = new StreamTokenizer(file2);

    int c1=0;
    int i=0;
    int numberOfTokensGenerated1 = 0;
    while(i != StreamTokenizer.TT_EOF){
    str1[c1]=streamTokenizer1.toString();
    c1++;
    i = streamTokenizer1.nextToken();
    numberOfTokensGenerated1++;
    }

    //System.out.println("total mispelled words " +c1);
    int c2=0;
    int j=0;
    int numberOfTokensGenerated2 = 0;
    while(j != StreamTokenizer.TT_EOF){
    str2[c2]=streamTokenizer2.toString();
    String w = streamTokenizer2.toString();
    // System.out.println(w);
    c2++;
    j = streamTokenizer2.nextToken();
    numberOfTokensGenerated2++;
    }

    int q=0;
    int k;
    if(c1==c2)
    {
    for( k=0;k<c1;k++)
    {
    if(str1[k].compareTo(str2[k])==0)
    {
    System.out.println("this string is eq" +str1[k]);
    }
    else
    { str3[q]=str2[k];
    q++;
    }
    System.out.println("
    Total Mispelled words"+q);
    System.out.println("
    Mispelled words");

    }



    for(i=0;i<q;i++)
    {
    System.out.println(str3[i]);
    }
    }



    }catch(IOException e)
    {
    e.printStackTrace();
    }

    }

    }
    }

Sign In or Register to comment.