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;
}
}