In: Computer Science
Write a driver to get a String input from keyboard and if the input string has less than 10 characters, throw a StringTooShortException.
public class StringTooShortException extends Exception
{
//-----------------------------------------------------------------
// Sets up the exception object with a
particular message.
//-----------------------------------------------------------------
public StringTooShortException()
{
super("String does
not have enough characters");
}
}
import java.util.Scanner; class StringTooShortExceptionTest { public static void main(String[] args) throws StringTooShortException { Scanner in = new Scanner(System.in); System.out.print("Enter a string: "); String s = in.nextLine(); if (s.length() < 10) throw new StringTooShortException(); } } public class StringTooShortException extends Exception { //----------------------------------------------------------------- // Sets up the exception object with a particular message. //----------------------------------------------------------------- public StringTooShortException() { super("String does not have enough characters"); } }