All,
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:
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 > 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;
}
Not sure if I really need to check that to prevent DOS attack, or Java usually takes care of that?
Thanks,