-2

I am making a login page, and i would like it if "Successfull login" appears in the console if the username and password match the database. So i simply added a System.out.println(); in the if statement, but it gets the error "unreachable statement". Why is that? Here is the loop:

if (user.equalsIgnoreCase(userFromDB) && hashedPass.equals(passFromDB)) {
        return "Correct username and password!";
        System.out.println("Login successfull using username \"" + user + "\"");
    }
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • 3
    A return statement is the last reached line of your method, as it stops the method execution. Everything you put after it is unreachable. – BackSlash Mar 15 '18 at 08:08
  • The return keyword is used to return from a method when its execution is complete. When a return statement is reached in a method, the program returns to the code that invoked it. – gargkshitiz Mar 15 '18 at 08:09
  • 2
    Possible duplicate of [Unreachable statement?](https://stackoverflow.com/questions/16139376/unreachable-statement) – Jean-Baptiste Yunès Mar 15 '18 at 08:11

2 Answers2

6

This is not a loop, it is a conditional statement and even if it would be a loop it woudln't change a thing.

This is an unreachable statement because return is the place where you get out of the method and return a value of an expression that is next to the return key word.

Yoda
  • 17,363
  • 67
  • 204
  • 344
0

Put your System.out.println() before the return statement. The return statement take the compiler out of the function and the println() statement won't be executed at all.

M.Lamine Lalmi
  • 78
  • 1
  • 2
  • 12