-1

I wanted to find out in options.bldata if firstLaunch= was written, for this I wrote the following code:

File file = new File("options.bldata");
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        
        String line = scanner.nextLine();
        if (line == "firstLaunch=") {
            
            System.out.println("123");
            
        }
        
    }

when it finds a line with firstLaunch= it should print 123, but I don't know why it doesn't print 123 even if firstLaunch= is in the file.

krystian
  • 1
  • 1

3 Answers3

2

Operator == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.

while (scanner.hasNextLine()) { 
        String line = scanner.nextLine();
        if (line.equals("firstLaunch=")) {   
            System.out.println("123");   
        }   
    }

Here is an article for more information

1

There is two wrong thing in the code.

  1. Never use double equals to compare strings. You need to use the equals() function from string to do so
  2. you want to test if the string firstLaunch= is in the line, not that your line is equals to firstLaunch=. For this, you can use line.contains("firstLaunch=")
leddzip
  • 99
  • 2
1

You should use equals() to compare strings. Or in your case, contains() because the line might have other stuff, not just what you want to find

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24