Home > Enterprise >  If loop inside or outside the method
If loop inside or outside the method

Time:05-28

I have a part of code what i want to upgrade.

                request.getLastName(), 
                request.getEmail(), 
                request.getPassword(), 
                AppUserRole.USER
                )
        );

String link = "http://localhost:8080/api/v1/registration/confirm?token="   token;
        emailSender.send(
                request.getEmail(),
                buildEmail(request.getFirstName(), link));

        return token;
    }

I want to create if, for giving a role ADMIN, I've tried to add if inside the appUserService and outside of it with another appUserService like downside, but its always give me an error. When String token = appUserService.signUpUser inside the if, it says that token is not defined in the String link... part of code. I want that it will be like that

if (request.getEmail() == "[email protected]")  {
                       String token = appUserService.signUpUser(new AppUser(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), AppUserRole.ADMIN));
                    } else {
                        String token = appUserService.signUpUser(new AppUser(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), AppUserRole.USER));
                    }

Write me please how it must be to work properly.

CodePudding user response:

Write this code to this:

String token = null;
if (request.getEmail().equals("[email protected]"))  {
    token = appUserService.signUpUser(new AppUser(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), AppUserRole.ADMIN));
} else {
    token = appUserService.signUpUser(new AppUser(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), AppUserRole.USER));
}

It should work.

First of all, your String comparison isn't right. You were comparing references instead of content. Secondly (which is your asking), you declared token in the if block, which can't be found outside of if and else block.

CodePudding user response:

Define token outside of if-else. Then assign value to it according to your conditions. Like this:

    String token;
    if (request.getEmail().equals("[email protected]"))  {
      token = appUserService.signUpUser(new AppUser(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), AppUserRole.ADMIN));
    } else {
      token = appUserService.signUpUser(new AppUser(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), AppUserRole.USER));
    }
    String link = "http://localhost:8080/api/v1/registration/confirm?token="   token;

This is called variable scope, you can read more about it here.

Also you should not compare string with ==, read more about that here.

CodePudding user response:

There is a lot of overlap between the two branches. In addition to the token, you can declare the role before the if statement (if loop is wrong):

    AppUserRole role;
    if (request.getEmail().equals("[email protected]"))  {
      role = AppUserRole.ADMIN;
    } else {
      role = AppUserRole.USER;
    }
    String token = appUserService.signUpUser(new AppUser(request.getFirstName(), request.getLastName(), request.getEmail(), request.getPassword(), role));
    String link = "http://localhost:8080/api/v1/registration/confirm?token="   token;

Now you don't have to repeat all the other code for creating an AppUser.

Even more concisely, you can use the ternary conditional operator:

    AppUserRole role = request.getEmail().equals("[email protected]") ? AppUserRole.ADMIN : AppUserRole.USER;
    String token = ...

Note I also fixed a problem with your string comparison, please read How do I compare strings in Java?.

  • Related