0

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() == "admins.mail@gmail.com")  {
                       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.

  • Please read how to post a minimal reproducable example: https://stackoverflow.com/help/minimal-reproducible-example Consider posting the whole class. – Laisender May 28 '22 at 11:15

3 Answers3

2

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

    String token;
    if (request.getEmail().equals("admins.mail@gmail.com"))  {
      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.

Chaosfire
  • 4,818
  • 4
  • 8
  • 23
1

Write this code to this:

String token = null;
if (request.getEmail().equals("admins.mail@gmail.com"))  {
    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.

Mukit09
  • 2,956
  • 3
  • 24
  • 44
  • = null is not necessary – Clashsoft May 28 '22 at 11:21
  • true. as it will get value in any of the blocks. But assigning null will not create any problem either. – Mukit09 May 28 '22 at 11:22
  • It may create confusion and if someone forgets to assign token in one of the branches, it will not be highlighted and lead to an NPE. – Clashsoft May 28 '22 at 11:28
  • if the token isn't assigned in any of the branches (if or else) and the token isn't assigned null as well while declaring, your code will not be compiled even. So don't think about what will be happened on runtime. @Clashsoft – Mukit09 May 28 '22 at 11:32
0

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("admins.mail@gmail.com"))  {
      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("admins.mail@gmail.com") ? 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?.

Clashsoft
  • 11,553
  • 5
  • 40
  • 79