samedi 20 février 2016

Why does if statement never equal "0" or "1" ? I print variable "line" and it is a "0" or "1". But IF statement never works. [duplicate]

This question already has an answer here:

/**
 * @(#)StringToFile.java
 *
 *
 * @author 
 * @version 1.00 2016/2/14
 */
package StringToFile;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;


public class StringToFile {

public static void main(String[] args) throws IOException {

 read("0");
}

public static String read(String msg2) throws IOException {

This is hard coded read of text file

List<String> lines = Files.readAllLines(Paths.get("g:/dungeontreasure/DMJ-solito-v2.12/test.txt"), Charset.defaultCharset());
for (String line : lines) {

msg2 is assigned what was read from text file. I hope.

msg2 = line; //  I assign msg2 what was read from file.

Now, I print out the contents of the text file. this part works.

System.out.println("line read:" + line); // I print out line

Now I check if the text file had a "1" in it, but this never works.

if (msg2==("1")) {
    System.out.println("check read: " + msg2);  // check to see what we read. But this never works.

        }

Now I check if the text file had a "0" in it, but it never works.

if (msg2==("0")) {
System.out.println("check read: " + msg2); // check to see what we read. 



        }

}  

return(msg2);
}

}

Aucun commentaire:

Enregistrer un commentaire