Question

(use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...

(use java)

Write a program that creates an exception class called StringTooLongException, designed tobe thrown when a string is discoveredthat has too many characters in it. In the main driver of the program, read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (say 20), throw, catch, and handle the exception. The exception is handled by printing an appropriate message, and the programwill continueprocessing more strings.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have created the StringTooLongException class and also override the toString( ) method. Though I have documented the code still if you have any doubt feel free to comment. I'll try my best to resolve them.

Assumption : For "Done", I am taking it as case insensitive i.e. If user entered "Done" then the program will exit but don't worry if you want it as case sensitive, I've commented the solution(see line 36).

=============================================

Code :   Main.java

import java.util.Scanner;
class StringTooLongException extends Exception
{
String str;
public StringTooLongException(String s)
{
// Call constructor of parent Exception
super(s);
str=s;
}
//Override toString method
public String toString()
{
return "String : "+str+" has more than 20 characters";
}
}
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s;
int len;
while(true)
{
try
{
//input
System.out.println("\nEnter the String : ");
s=sc.next();
len=s.length();
  
//checking for exception
if(len>20)
throw new StringTooLongException(s);
/*checking if user entered done. This is case insensitive
for case sensitive use s.equals("DONE") */
if(len==4 && s.equalsIgnoreCase("Done"))
break;
//tell the user that string is valid
System.out.println("\n"+s+" is valid.");
}
catch(StringTooLongException ob)
{
System.out.println("\nException Thrown");
System.out.println(ob);
}
}
sc.close();
}
}

====================================================

Syntax :

=============================================

Output :

Add a comment
Know the answer?
Add Answer to:
(use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates...

    java punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates an exception class called StringTooLongException, designed to be thrown when a string is discovered that too many characters in it. In the main driver of the program, read strings from th user until the user enters "DONE". If a string is entered that has too many chara (say 20), throw the exception. Allow the thrown exception to terminate the program.

  • JAVA Problem Description: For this assignment, you will be writing your own exception class and you...

    JAVA Problem Description: For this assignment, you will be writing your own exception class and you will handle run-time exceptions resulting from one particular input situation. Specifics: 1) Design and implement a program that has an exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. 2) In the main driver of the program (call this class MyExceptionTest), read strings from the user until the user enters “DONE”. If a...

  • Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters...

    Q1: Write a class that throws an ExtraneousStringException when a string has more than 30 characters in it (see ExtraneousStringException.java. This java file does not need to be modified). Copy paste the contents of ExtraneousStringException.java into a new .java file, uncomment the commented out code, and write your code inside the main method. In the driver class, keep reading strings from the user until the user enters “DONE.” You may read your input from a file, or use the console....

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is...

    QUESTION 1 If an exception is thrown None listed all listed execution terminates if there is no catch block handling the same data type as the exception thrown execution continues if the catch block handles the same data type as the exception thrown 10 points    QUESTION 2 An exception is handled using an if-else statement an assignment statement a loop statement a try-catch block 10 points    QUESTION 3 In program 9.4, what is the data type being thrown?...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale. What is the answer...

    Source material: Object-Oriented Data Structures Using Java, 3rd Edition by Nell Dale. What is the answer to exercise 13 in chapter 3 (page 232)?? "A. Create a "standard" exception class called ThirteenException. B. Write a program that repeatedly prompts the user to enter a string. After each string is entered the program outputs the length of the string, unless the length of the string is 13, in which case the ThirteenException is thrown with the message "Use thirteen letter words...

  • Java Question, I need a program that asks a user to input a string && then...

    Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...

  • Be sure to include a message to the user explaining the purpose of the program before...

    Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Be sure to include information about which document codes are valid. Within the main method, please complete the following tasks in java program: Create a loop that allows the user to continue to enter two-character document designations until a sentinel value is entered (ex. “XX”). If the user enters a valid document code, please echo that value back...

  • Write a Java program that reads a series of strings from a user until STOP is...

    Write a Java program that reads a series of strings from a user until STOP is entered. Each input consists of information about one student at the ABC Professional School. The input consists of the student’s last name (the first 15 characters with extra blanks on the right if the name is not 15 characters long), the student’s number (the next 4 characters, all digits, a number between 1000 and 5999), and the student’s program of study (one character, either...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT