title: Java console input validation to prevent DOS attack
body: <p>All, </p>
  
  <p>I have written a java code that needs to check an entered text by the user using the console, I want that the code tests the entered line to not exceed for example 20 letters,
  I wrote it as follow:</p>
  
  <pre><code>String getName() {
  
  boolean badName = true;
  String Name = "";
  Scanner console = new Scanner(System.in);
  
  
  while (badName ){
        System.out.println("Please enter your first name ");
            Name = console.nextLine(); 
  
       //^ I want to check this string length while the user enters the line 
       // to prevent DOS attack when an attacker tries to enter very large line
  
      if (console.nextLine().length &gt; 20) {
      //^ I tried  this but could not get the string value after this condition validated, 
      // I dont want to store it in a variable to not cause DOS attack.
             System.out.println("please enter valid name!!!");
             continue;
           }
  
         if (! Name.matches ("[a-zA-Z_]+")) {
            System.out.println("the name contains invalid character, please try again :");
           continue;
         } 
        if (Name.matches ("[a-zA-Z_]+")){
        badName = false;
          }
  
       }
  
  
      return Name;
  }
  </code></pre>
  
  <p>Not sure if I really need to check that to prevent DOS attack, or Java usually takes care of that?</p>
  
  <p>Thanks,</p>