As my title says. I need to search through a file for a string. When its found, I need the next line. It's a file like this:
hello
world
When "hello" is found, "world" needs to be returned.
File file = new File("testfile");
Scanner scanner = null;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (scanner != null) {
String line;
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line == "hello") {
line = scanner.nextLine();
System.out.println(line);
}
}
}
It reads through the file but it doesn't find the word "hello".