Skip to content

Instantly share code, notes, and snippets.

Created Aug 26, 2018
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.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()
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) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment