pauln91
pauln91

Reputation: 31

Java reading a string from file not equal to itself

I am new to programming and I cannot for the life of me figure out why aa does not equal bb in the following. I need to extract a file path string from a text file of settings. I read in the 9th line from a txt file as a string like so to get the path:

String content = new Scanner(new File("settingsfilepath.txt")).useDelimiter("\\Z").next();
String[] textStr = content.split("\n");

aa = textStr[9];
bb = "testpath";

    if(aa.equals(bb)){
        print("Contents of both strings are same");
    }else{
        print("Contents of strings are different");
    }

print(textStr[9]);
print(bb);

OUTPUT when printing aa is: testpath

OUTPUT when printing bb is: testpath

RESULT: "Contents of strings are different"

I need the variable aa to equal the string bb because when I use a save function [savefunction(filetype,filepath);]

somesavefunction(filetype,aa); error problem with path

somesavefunction(filetype,bb); this works

Are there two different types of strings? Maybe I am not reading in the file path from the text file as a string properly? I feel so stupid :( I hope this question makes sense as I had a hard time trying to explain it in this post. Thanks for any and all help.

Upvotes: 2

Views: 1591

Answers (3)

Jigar Joshi
Jigar Joshi

Reputation: 240966

I doubt you have white space, try using trim() and change your condition to

aa.trim().equals(bb.trim())

Upvotes: 1

JamesB
JamesB

Reputation: 7894

Assuming the 9th line is what you want, you should set aa to the 8th array element as arrays have a zero-based index.

aa = textStr[8];

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201527

I recommend you do a

System.out.printf("aa = '%s', bb = '%s'%n", aa, bb);

I suspect there is white space in one or both strings.

Upvotes: 1

Related Questions