Question

Assignment 2: Exceptions Mr. Mike wants to check whether the student number and GSM number entered...

Assignment 2: Exceptions
Mr. Mike wants to check whether the student number and GSM number entered by the student to register for attending a conference is valid or not.
Write a program in Java to read the Student ID and GSM Number of a student. If the Student ID does not start with the characters ST or if the Mobile Number does not contain exactly 8 digits, throw a user defined exception InvalidDetailsException.
If the Mobile Number contains any character other than a digit, throw a NumberFormatException. If the student ID contains any special character other than digits and alphabets, throw a NoSuchElementException. If the details entered are valid, display the message ”Correct details are entered!!!” otherwise display “Entered invalid details!!!!”
Sample Test case:
Enter Student Number:
ST1402
Enter Student GSM Number:
98989898
Output:
”Correct details are entered!!!”
===========================================
Enter Student Number:
1402ST
Enter Student GSM Number:
999898998
Output:
Student Id is invalid !!!
Student GSM number is invalid !!!
“Entered invalid details!!!!”
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Required code is as follows.

import java.util.Scanner;

// user defined exception InvalidDetailsException

class InvalidDetailsException extends Exception
{
   public InvalidDetailsException(String s)
   {
       // call constructor of parent Exception.
       super(s);
   }
}

class exception
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       String studentNumber = "";
       String gsmNumber = "";
       boolean error = false;
       try
       {
           System.out.println("Enter Student Number:");
           studentNumber = sc.next();
           if(studentNumber.charAt(0)!='S' || studentNumber.charAt(1)!='T')
               throw new InvalidDetailsException("Student Id is invalid !!!");
       }
       catch(InvalidDetailsException e)
       {
           error = true;
           System.out.println(e.getMessage());
       }
       try
       {          
           System.out.println("Enter Student GSM Number:");
           gsmNumber = sc.next();
          
           if(gsmNumber.length()!=8)
               throw new InvalidDetailsException("Student GSM number is invalid !!!");
       }

       catch(InvalidDetailsException e)
       {
           error = true;
           System.out.println(e.getMessage());
       }

       try
       {
           for(int i=0;i<studentNumber.length();i++)
           {
               if (!((studentNumber.charAt(i)>='a' && studentNumber.charAt(i)<='z') || (studentNumber.charAt(i)>='A' && studentNumber.charAt(i)<='Z') || (studentNumber.charAt(i)>='0' && studentNumber.charAt(i)<='9')))
               {
                   throw new NoSuchFieldException();
               }
           }
       }

       catch(NoSuchFieldException e)
       {
           error = true;
           System.out.println(e.getMessage());
       }

       try
       {
           for(int i=0;i<8;i++)
           {
               if(!(gsmNumber.charAt(i)>='0' && gsmNumber.charAt(i)<='9'))
               {
                   throw new NumberFormatException();
               }
           }
       }      

       catch(NumberFormatException e)
       {
           error = true;
           System.out.println(e.getMessage());
       }

       if(error)
           System.out.println("Entered invalid details!!!!");
       else
           System.out.println("Correct details are entered!!!");

   }
}

//Refer above image for sample output

//Ask your doubts if any in the comments section below.

// Please leave your rating.

Add a comment
Know the answer?
Add Answer to:
Assignment 2: Exceptions Mr. Mike wants to check whether the student number and GSM number entered...
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
  • a) Create an abstract class Student. The class contains fields for student Id number, name, and...

    a) Create an abstract class Student. The class contains fields for student Id number, name, and tuition. Includes a constructor that requires parameters for the ID number and name. Include get and set method for each field; setTuition() method is abstract. b) The Student class should throw an exception named InvalidNameException when it receives an invalid student name (student name is invalid if it contains digits). c) Create three student subclasses named UndergradStudent, GradeStudent, and StudentAtLarge, each with a unique...

  • Output Enter base: 2 supply a list of digits separated by space: 1 0 0 1...

    Output Enter base: 2 supply a list of digits separated by space: 1 0 0 1 The value for Base = 2 and digits = 1 0 0 1 is 9 Enter base: 16 supply a list of digits separated by space: 99 All digits must be in the range [0,n) Enter base: 16 supply a list of digits separated by space: 9 9 The value for Base = 16 and digits = 9 9 is 153 Enter base: 2...

  • Hello I'm having a bit of trouble with this assignment. Any help would be appreciated! Overview...

    Hello I'm having a bit of trouble with this assignment. Any help would be appreciated! Overview You must implement a Java class which simulates a Student object. These Student objects will represent grade records for students. Variable Details  name : private String : represents the Student’s name  sid: private String : represents the Student’s ID number  quizzes: private double[] : an array whose size is equal to NUM_QUIZZES. Each element will be used to store one of...

  • In this assignment you are going to handle some basic input operations including validation and manipulation,...

    In this assignment you are going to handle some basic input operations including validation and manipulation, and then some output operations to take some data and format it in a way that's presentable (i.e. readable to human eyes). Functions that you will need to use: getline(istream&, string&) This function allows you to get input for strings, including spaces. It reads characters up to a newline character (for user input, this would be when the "enter" key is pressed). The first...

  • CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!)....

    CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!). Can be resubmitted up to two times until Session 6 if your first version is submitted by session 3. In this project, you create two classes for tracking students. One class is the Student class that holds student data; the other is the GradeItem class that holds data about grades that students earned in various courses. Note: the next three projects build on this...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • Here’s your task: You’re going to create exactly two classes. One class will act as the...

    Here’s your task: You’re going to create exactly two classes. One class will act as the functional server. The main method of this server will setup the connection on a localhost, find the two clients, let them know which one is Player 1 and which one is Player 2 (depending on the order they connect), and then manage the game as it’s being played. This is important: For each player’s turn they should start by displaying their current score to...

  • Use program control statements in the following exercises: Question 1 . Write pseudocode for the following:...

    Use program control statements in the following exercises: Question 1 . Write pseudocode for the following: • Input a time in seconds. • Convert this time to hours, minutes, and seconds and print the result as shown in the following example: 2 300 seconds converts to 0 hours, 38 minutes, 20 seconds. Question 2. The voting for a company chairperson is recorded by entering the numbers 1 to 5 at the keyboard, depending on which of the five candidates secured...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

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