Niko H
Niko H

Reputation: 63

How come two Strings are not equals when they print the same thing?

this may be a silly question but I honestly have no idea what I'm doing wrong. I created a class that reads a file and prints every words in it. First I created an instance of my class called 'lines'. I ran my program to see what lines.toString() prints, and then copy/pasted the result. I stored the result in a String variable called 'result' and then I compared 'result' and 'lines'.

As it turns out, lines.equals(result) is false.. Why is this? I only copy and pasted the result and then compare it to the original. They literally print the same thing, so there should be no difference in white space or anything like that. I know its a bit long, but my code is below if you would like to take a look at it. I appreciate anyone who would help me. Thank you!

public Document(String fileName) throws FileNotFoundException {
    File file;
    Scanner docScanner;

    String curLine;
    numLines = 0;
    numWords = 0;

    file = new File(fileName);
    docScanner = new Scanner(file);

    while (docScanner.hasNext()) {
        docScanner.nextLine();
        numLines++;
    }

    docScanner = new Scanner(file);
    words = new String[numLines][];

    for (int i = 0; i < words.length; i++) {
        curLine = docScanner.nextLine();
        words[i] = curLine.trim().split("\\s");

        if (words[i][0].equals("")) {
            words[i] = new String[0];
        } else {
            for (int j = 0; j < words[i].length; j++) {
                numWords++;
            }
        }
    }
}

public String getLine(int lineNum) throws NoSuchLineException {
    String result = "";
    for (int i = 0; i < words[lineNum].length; i++) {
        result = result + words[lineNum][i] + " ";
    }
    return result;  
}

public String toString() {
    String result = "";
    for (int i = 0; i < words.length; i++) {
        result = result + getLine(i) + "\n";
    }
    return result;
}

public static void main(String[] args) throws FileNotFoundException {
    Document lines = new Document("file.txt");

    String result = "Mary had \r\n" + 
            "a little lamb \r\n" + 
            "Its fleece was \r\n" + 
            "white \r\n" + 
            "as snow ";

    if (lines.toString().equals(result)) {
        System.out.println("They are equal");
    } else {
        System.out.println("Not equal");
    }
}

file.txt is below:
Mary had 
        a little lamb
Its fleece was 
    white 
as snow

Upvotes: 0

Views: 1019

Answers (2)

Kamil Piwowarski
Kamil Piwowarski

Reputation: 524

As suggested in the comment section: rewriting my comment as an answer.

It seems the cause of the problem is inside implementation of the toString method:

for (int i = 0; i < words.length; i++) {
    result = result + getLine(i) + "\n";
}

Newline character is always appened, regardless if in the middle of the loop, or at the last line, while the test string lacks the newline character in the last line.

Taking this into account, the for loop could look something like:

public String toString() {
    String result = "";
    for (int i = 0; i < words.length - 1; i++) {
        result = result + getLine(i)+"\r\n";
    }
    return result + getLine(words.length - 1);
}

Please note I've also changed unix line ending \n into windows equivalent \r\n you used in the result String in the sample, as those are not equal.

In most cases it is better to use System.lineSeparator() method instead of manually writing the separator in the string as under various circumstances your strings could be wrongly displayed on different systems if you use the wrong one.

My version of the loop can of course be further improved, for example by introducing a variable in place of words.length - 1 for better readability, and more importantly: usage of StringBuilder instance initialized before start of the loop instead of String for the concatenation.

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 339303

if (lines.equals(result)) {

lines is a Document, while result is a String.

So they cannot be equal. Comparing Document and String is like comparing apples and oranges.

I do not know what class is Document, as you neglected to mention.

Regarding String, its equals method requires an object of the same class. To quote the documentation:

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Upvotes: 2

Related Questions