Question

In: Computer Science

Write a driver to get a String input from keyboard and if the input string has...

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");
    }
}

Solutions

Expert Solution

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");
    }
}


Related Solutions

Write a program that prompts the user to input their first name from the keyboard and...
Write a program that prompts the user to input their first name from the keyboard and stores them in the variable "firstName". It does the same for last name and stores it in the variable "lastName". It then uses strcat to merge the two names, separates them by a space and stores the full name into a string variable called "fullName". In the end, the program must print out the string stored within fullName. ANSWER IN C LANGUAGE ! You...
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard...
in c++ QUESTION 4: Write the code to do the following: -read input from the keyboard by displaying the message on the screen to ask and read the following information: * customer ID (string) * customer name (string)                                                        * balance (float) -open output file customer .txt to write -Write to the output file customer.txt the following information:                 Customer ID – customer name – balance For example: 1561175753 - James Smith – 1255.25 -close file QUESTION 5: -create one notepad...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Fix this broken code? # Get our input from the command line import sys string =...
Fix this broken code? # Get our input from the command line import sys string = sys.argv[1] # Your code goes here if string 'Bingo' print('Missed') else: print('Hit!')
Write a C++ function called parse that reads one line of user input from the keyboard...
Write a C++ function called parse that reads one line of user input from the keyboard and creates an array of the strings found in the input.  Your function should be passed the array and a reference variable that is to be assigned the length of the array.  Prompt the user and read the input from within the function. For example:  If the user inputs copy this that, the resulting array would have length 3 and contain the strings “copy”, “this”, and “that”....
Write an Assembly code to input two integer numbers from keyboard, computes the division of two...
Write an Assembly code to input two integer numbers from keyboard, computes the division of two numbers WITHOUT using division operator and print out the reminder and quotient to the screen. Note: program using “division operator” will earn no credit for this task. You are ALLOWED to use the “print” and “read” macro
Input a phrase from the keyboard and output whether or not it is a palindrome. For...
Input a phrase from the keyboard and output whether or not it is a palindrome. For example: 1. If the input is Madam I'm Adam Your output should be "Madam, I'm Adam" is a palindrome 2. If your input is Going to the movies? Your output should be "Going to the movies?" is not a palindrome Note the quotes in the output line. Code language java using JGrasp
Input a phrase from the keyboard. If the last letter in the phrase is "a" or...
Input a phrase from the keyboard. If the last letter in the phrase is "a" or "e" (uppercase or lowercase) then output the last letter 3 times. ELSE If the first letter in the phrase is either "i", "o", or "u" (uppercase or lowercase) then output the length of the phrase In all other cases print "no vowel" Code language is java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT