Skip to content

Instantly share code, notes, and snippets.

@ThakurPriyanka

ThakurPriyanka/login

Created Aug 26, 2018
Embed
What would you like to do?
package com.knoldus.user;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Scanner;
import java.util.Set;
public class Login {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the username");
String username = sc.nextLine();
System.out.println("Please enter the password");
String password = sc.nextLine();
try {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
User newUser = User.builder()
.username(username)
.password(password)
.build();
Set<ConstraintViolation<User>> violations = validator.validate(newUser);
for (ConstraintViolation<User> violation : violations) {
if (!("".equals(violation.getMessage()))) {
throw new RuntimeException(violation.getMessage());
}
}
System.out.println("User created");
System.out.println(newUser.getUsername() + " Created\n");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment