0

I am new to coding and I am trying to make a program that matches two text files. For the first line, I match the first character and the second line I match two characters and so on until one of the files ends.

Here is the problem. If the line does not have enough characters you only compare what is present, if both lines 99 have "abc" then it would be considered a match.

I can't seem to fulfill this requirement without it throwing an error because charAt() is reading a null if the number of characters is less than the number of lines. Thanks for the help!

Code:

public static void main(String[] args) throws IOException
{
    boolean flag;
    File f = new File(args[0]);
    Scanner sc = new Scanner(f);
    File f2 = new File(args[1]);
    Scanner sc2 = new Scanner(f2);
    int line = 1;
    String t1 = "";
    String t2 = "";
    String work = "";

    while(sc.hasNext() && sc2.hasNext())
    {
        String line1 = sc.nextLine().toUpperCase();
        String line2 = sc2.nextLine().toUpperCase();
        line1 = line1.trim();
        line2 = line2.trim();
        for(int i = 0; i < line; i++)
        {
            if(line1.charAt(i) != line2.charAt(i))
            {
                work = "no";
                t1 = Character.toString(line1.charAt(i));
                t2 = Character.toString(line2.charAt(i));
            }
            else
            {
                work = "";
                t1 = Character.toString(line1.charAt(i));
                t2 = Character.toString(line2.charAt(i));
            }
        }
        if(work == "")
        {
            System.out.println("Line # " + line + ": Matching " + line + " character/s true " + "**" + line1 + "**" + " and " + "**" + line1 + "**");

        }
        else
        {
            System.out.println("Line # " + line + ": Matching " + line + " character/s false " + "**" + line1 + "**" + " and " + "**" + line2 + "**");
        }
        line += 1;
    }
}
Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
  • 2
    Dont `if(line1.charAt(i) != line2.charAt(i))` , use `if(line1.charAt(i).equals(line2.charAt(i)))` – Sekula1991 Nov 07 '15 at 01:23
  • 3
    [`use equals() to compare`](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). @Sekula1991 It primitive types.so `==` is fine – sam Nov 07 '15 at 01:24
  • You gotta check if the result of charAt() is null, then handle accordingly – Eric Guan Nov 07 '15 at 01:30

2 Answers2

0

You need to check the length of your line1 and line2 versus your i variable before you call char at past the end of the last letter of the line.

Something like if(line1.length() < i)

String.length()

Travis
  • 2,105
  • 2
  • 26
  • 35
0

1st check if both lines are of equal length as follows (and change variable work as boolean instead of String). It helps with performance since boolean takes 1 byte and String more than that.

if (line1.length() > line2.length()) {
    work = false;
} else if (line1.length() < line2.length()) {
    work = false;
} else if (line1.charAt(i) != line2.charAt(i)) {
    work = false;
} else {
    work = true;
}

Or:

if (line1.length() != line2.length()) {
    work = false;
} else {
    work = true;
}

Or:

if (line1.length() == line2.length()) {
    work = true;
} else {
    work = false;
}

Then

if(work) {//This is the same as if (work == true)
    System.out.println("Line # " + line + ": Matching " + line + " character/s true " + "**" + line1 + "**" + " and " + "**" + line1 + "**");
} else {
    System.out.println("Line # " + line + ": Matching " + line + " character/s false " + "**" + line1 + "**" + " and " + "**" + line2 + "**");
}

But if you really really want / need to use work variable as String then don't compare it with == but work.equals("") or "".equals(work) (the second one prevents NullPointerException.

Read more about .equals(), Java == vs equals() and How do I compare Strings in Java. And about "".equals(variable) vs variable.equals("")

Also you might want to break or continue your loop if at any position the 2 lines don't match at any letter.

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89