Question

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 to the user. (Note: this is an additional requirement to those listed in the book.)

The exception message should provide clear feedback to the user (ex. “Invalid Document Designation”).

Should the user enter an invalid document designation, ensure the program will catch and handle the exception and the program will continue to function until the user chooses to end it using the sentinel value.

Include clear comments throughout the program that explain the well named variables and what functionality the various lines/small sections of code are completing as related to the execution of the program.

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

Please find my implementation:

import java.util.Scanner;

public class Driver {

   public static void main(String[] args) {

       // TODO Auto-generated method stub

       String str=null;

       int i=0;

       Scanner sc=new Scanner(System.in);

       System.out.println("Please enter the two character document designation,\n Please use XX to end the program");

       str=sc.nextLine();

       while(!str.equals("XX")) // If the document name is XX capital case, it will be exit

       {

           if(i>0)

           {

               System.out.println("Please enter the two character document designation,\n Please use XX to end the program");

               str=sc.nextLine();

           }

           // Reading the document name

           try

           {

               if(str.length()!=2) // If the length is not 2 then its a invalid document

               {

                   throw new InvalidDocumentCodeException("Invalid Document Designation"); // throwing the custom exception

               }

               else

               {

                   System.out.println("Open document"); // If its 2 then you can open document

               }

           }

           catch(InvalidDocumentCodeException ex) // catching the custom exception

           {

               ex.printStackTrace();

           }

           i++;

       }

   }

}

// Creating a custom Exception class

class InvalidDocumentCodeException extends Exception {

   // Constructor with some message

   public InvalidDocumentCodeException(String message)

   {

       super(message);

   }

}

Please enter the two character document designation, Please use XX to end the program 1223 com.InvalidDocumentCodeException:

Add a comment
Know the answer?
Add Answer to:
Be sure to include a message to the user explaining the purpose of the program before...
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
  • Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the...

    Wrote a program called ExceptionalDivide that asks the user for 2 integer values and displays the quotient of the first value divided by the second value. Make sure the your program reads in the two input values as ints. Your program must catch either of two exceptions that might arise and crash your program java.util.InputMismatchException //wrong data type inputted java.lang.ArithmeticException //divide by zero error Once you catch these exceptions, your program must print out a message explaining what happened and...

  • Write a program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

  • Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that...

    Case Problem 11 - 1: Modify the GreenvilleRevenue program created in the previous chapter so that it performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. Use exception-handling techniques to ensure a valid value and display the error message: Number must be between 0 and 30 The program prompts the user for talent codes. Use exception-handling techniques to ensure a valid code and...

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

  • JAVA 2. (8 points) Write a code segment which prompts the user to enter the price...

    JAVA 2. (8 points) Write a code segment which prompts the user to enter the price of a tour and receives input from the user. A valid tour price is between $52.95 and $259.95, inclusive. Your program should continue to repeat until a valid tour price has been entered. If the user enters an invalid price, display a message describing why the input is invalid. You may assume that the user only enters a real number. The user will not...

  • For practice using exceptions, this exercise will use a try/catch block to validate integer input from...

    For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...

  • write a c++ code be sure the user only enter A or a for packageA, B...

    write a c++ code be sure the user only enter A or a for packageA, B or b for packageB, C or c for packageC. If the user inputs some other value display an error message saying " invalid out". When you ask the user to input the number of minute and the user enters a negative value you should display the same error message "invalid input" in both cases, after you display the error message the program should stop...

  • Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an...

    You are to write a program (BookExceptionsDemo.java) that will create and, using user input, populate an array of instances of the class Book. The class Book is loaded in our Canvas files: Book.java The user will enter a number n (n must be > 0, trap the user until they input a valid value for n), Your program will declare and create an array of size n of instances of the class Book. The user will be asked to enter...

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