Design a program that creates a StringTooLongException, say greater than 20 char
ID: 668696 • Letter: D
Question
Design a program that creates a StringTooLongException, say greater than 20 characters and handles the error with a message and continues processing more strings. Create another exception called InvalidDocumentCodeException which is thrown when an improper designation for adocument is encountered. All documents are given a 2 character designation, starting with U, C, or P. If a document designation is encountered that does not fit the description, an exception is thrown and handles the exception and continue the processing.
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone{
public static boolean InvalidDocumentCodeException(String s){
if (s.length() > 2) return true;
if (s.charAt(0) == "U" || s.charAt(0) == "C" || s.charAt(0) == "P") return false;
return true;
}
public static boolean StringTooLongException(String s){
if (s.length() > 20) return true;
return false;
}
public static void main (String[] args) throws java.lang.Exception{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Line separated by space : ");
String[] arr = sc.nextLine().split(" ");
for (int i =0; i < arr.length; i++){
if (StringTooLongException(arr[i]) == true){
System.out.println(arr[i]+" is too long");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.