0
/**
 * @(#)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);
}

}
captainx
  • 25
  • 7

1 Answers1

0

If you need to compare objects use always the method .equals(), not the operator ==.

Using the operator == test for the same object (same location in memory), instead the method .equals() check for the same content.

So instead of

if (msg2==("1")) {

your code should be

if (msg2.equals("1")) {
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56