Thursday, 15 August 2013

Java: Identical Strings don't match? [duplicate]

Java: Identical Strings don't match? [duplicate]

This question already has an answer here:
How do I compare strings in Java? 28 answers
I have a Scanner that gets a String, which should always be the same, then
an if statement that says that if that String equals another String (which
it always should), then to do a certain task. However, even when the two
Strings match, the code in the if statement is not completed. Here is the
relevant code:
Notes- in 'cipherFile', the first word is 'lfdkj', and I always get the
"This is not a valid code created by this program. Please try again,
making sure you have the entire code." message that is only supposed to
happen if the Strings don't match...
try {
PrintStream oFile = new PrintStream("Deciphered.txt");
deciphered = new File("Deciphered.txt");
Scanner scan = new Scanner(cipherFile);
readRot = scan.next();
if (readRot == "lfdkj") {
cipherWord = scan.next();
l = cipherWord.length();
charScan = new Scanner(cipherWord);
charScan.next();
while (scan.hasNext()) {
cipherWord = scan.next();
l = cipherWord.length();
charScan = new Scanner(cipherWord);
for (int i = 0; i < cipherWord.length(); i++) {
cipherChar = cipherWord.charAt(i);
// ...
oFile.print(cipherChar);
}
oFile.print(" ");
}
}
// ...
else {
oFile.print(readRot);
oFile.print("This is not a valid code created by this program.
Please try again, making sure you have the entire code.");
}
scan.close();
displayDeciphered();
} catch (IOException e) {
JOptionPane.showMessageDialog(frame, "I/O error in file\n\n "
+ cipherFile.getName() + "\n\nThis program will close",
"I/O Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}

No comments:

Post a Comment