KnowledgeBoat Logo

Computer Applications

A university student's registration number follows the format:

<CourseCode><Year><CollegeCode><RollNumber>

where

ComponentDescription
CourseCodeA 3-letter code representing the course (e.g., CSE for Computer Science, ECE for Electronics & Communication)
YearThe last two digits of the admission year.
CollegeCodeA 3-digit code representing the college
RollNumberA 4-digit unique student roll number.

Examples

Registration NumberCourse CodeAdmission YearCollege CodeRoll Number
CSE240011023CSE240011023
ECE252104297ECE252104297
ASE230277259ASE230277259

Define a class that accepts a student's registration number as input, extracts the relevant details, and displays them in the specified format.

import java.util.Scanner;

public class KboatStuRegNum
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter reg. no.: ");
        _______(1)_________
        
        int l = n.length();
        if (l != 12) {
            System.out.println("Invalid reg. no.");
            System.exit(0);
        }   
        
        _______(2)_________
        _______(3)_________
        _______(4)_________
        _______(5)_________
        
        System.out.println("Course Code    : " + cc);
        System.out.println("Admission Year : " + yr);
        System.out.println("College Code   : " + cl);
        System.out.println("Roll Number    : " + rNo);
    }
}

Java String Handling

3 Likes

Answer

  1. String n = in.nextLine();
  2. String cc = n.substring(0, 3);
  3. String yr = n.substring(3, 5);
  4. String cl = n.substring(5, 8);
  5. String rNo = n.substring(8);

Explanation

import java.util.Scanner;

public class KboatStuRegNum
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter reg. no.: ");
        String n = in.nextLine();
        
        int l = n.length();
        if (l != 12) {
            System.out.println("Invalid reg. no.");
            System.exit(0);
        }   
        
        String cc = n.substring(0, 3);
        String yr = n.substring(3, 5);
        String cl = n.substring(5, 8);
        String rNo = n.substring(8);
        
        System.out.println("Course Code    : " + cc);
        System.out.println("Admission Year : " + yr);
        System.out.println("College Code   : " + cl);
        System.out.println("Roll Number    : " + rNo);
    }
}

Variable Description Table

Variable NameData TypePurpose
inScannerAn instance of the Scanner class used to read input from the user.
nStringStores the registration number entered by the user.
lintStores the length of the registration number entered by the user.
ccStringExtracts the course code from the registration number (first 3 characters).
yrStringExtracts the admission year from the registration number (4th and 5th characters).
clStringExtracts the college code from the registration number (6th to 8th characters).
rNoStringExtracts the roll number from the registration number (last 4 characters).

Program Explanation

Let’s take a closer look at this Java program to understand how it works:

1. Importing Scanner:

  • The program starts by importing the java.util.Scanner class, which is used for reading input from the user. The Scanner class is a part of Java's standard library that allows us to capture input from various input sources, including the keyboard.

2. Defining the Main Class & main Method:

  • The class is named KboatStuRegNum. It contains the main method which is the entry point of the program. It is where the program begins execution.

3. Reading Input:

  • A Scanner object named in is created to capture user input. The program then prompts the user to "Enter reg. no." with System.out.print.

4. Getting the Registration Number:

  • Using in.nextLine(), it reads a line of text input from the user and stores it in the String variable n.

5. Validation:

  • The program checks if the length of the registration number (n.length()) is exactly 12 characters long. If the length isn't 12, it displays "Invalid reg. no." and terminates the program using System.exit(0). This ensures the input adheres to the expected format.

6. Extracting Information:

  • Assuming the input is valid (i.e., 12 characters), the program extracts different parts of the registration number using the substring method:
    • cc: Extracts the first three characters (n.substring(0, 3)) representing the course code.
    • yr: Extracts the next two characters (n.substring(3, 5)) representing the admission year.
    • cl: Extracts characters six to eight (n.substring(5, 8)) which are expected to represent the college code.
    • rNo: Extracts the remaining characters from the ninth to twelfth position (n.substring(8)) which are considered the roll number.

7. Displaying the Components:

  • The program then outputs the separated components of the registration number to the console using System.out.println(). This includes:
    • "Course Code : " followed by the extracted course code.
    • "Admission Year : " followed by the extracted admission year.
    • "College Code : " followed by the extracted college code.
    • "Roll Number : " followed by the extracted roll number.

Output

A university student's registration number follows the format: Practice Test ICSE Computer Applications Class 10
A university student's registration number follows the format: Practice Test ICSE Computer Applications Class 10

Answered By

3 Likes


Related Questions