Question

punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates an exception class called StringTooLon

java

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

import java.util.Scanner;

//exception class for long string
class StringTooLongException extends Exception
{
public StringTooLongException()
{
//display message on the computer screen
super("The string has more than 20 characters");   
}
}

public class Main
{
public static void main(String[] args) throws StringTooLongException
{
//variable declaration and initialization
int maxLength = 20;
String inputString;
  
Scanner input = new Scanner(System.in);
  
while(true)
{
//try block
try
{
//display message on the computer screen
System.out.println("Enter a string (enter DONE to finish): ");
inputString = input.nextLine();
  
if(inputString.equalsIgnoreCase("DONE"))
{
return;
}
  
if(inputString.length()>maxLength)
{
throw new StringTooLongException();
}
  
}
  
//catch block
catch(StringTooLongException e)
{
System.out.println(e.getMessage());
}
}
}
}

OUTPUT:

Enter a string (enter DONE to finish): hello hi Enter a string (enter DONE to finish): hello hi hello hi hello hi The string

Add a comment
Know the answer?
Add Answer to:
java punu ile 20 maze from chapter 12. 2. StringToolong.java → Write a program that creates...
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
  • (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.

  • 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...

  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • 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...

  • Write a Java Program to allow a user to guess a person’s age until they get...

    Write a Java Program to allow a user to guess a person’s age until they get it correct. After each guess, the user must be told whether their guess was correct, too high or too low. Once the user enters the correct age, the program must display how many guesses it took the user to guess the correct age.

  • 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....

  • JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour...

    JAVA 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock like 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). Provide a default constructor, a set method which is given a String (eg, “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate (in your opinion) to the time. Please see the NOTE below concerning input validation and...

  • Write a program that allows students to schedule appointments at either 1, 2, 3, 4, 5,...

    Write a program that allows students to schedule appointments at either 1, 2, 3, 4, 5, or 6 o�clock pm. Use an array to store each of the six (strings) of names for the 6 time slots. Write a loop that iterates as long as the array has a freespace (program should end when all time slots are taken). Within a try block, allow the user to enter a time and a name. If the time is free, put the...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

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