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

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

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

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

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

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

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

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

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

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