In: Computer Science
Programming language is Java.
Consider you have already created a validateString(String str) method that validates a String to ensure it contains exactly 4 characters.
Create code that calls this method, sending user input as an argument (actual parameter). The user should be continuously prompted to enter a different string and informed of an error if the method returns false.
Code:
import java.util.Scanner;
class Main {
public static boolean validateString(String str)
{
if(str.length() == 4)
{
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str;
boolean x = true;
while(x)
{
System.out.println("Enter String: ");
str = sc.next();
x = validateString(str);
System.out.println(x);
}
}
}
Screenshots: