Java Program of Marathon Registration

Marathon Registration

Question:

To participate in a marathon competition we need to register ourself by providing the following details.

Name

Age

Gender

Contact no

After obtaining all the details, display the message as “Registered Successfully”, if any of the input is incorrect handle the exception (InputMismatchException) and display as “Invalid Input”.

Partial Code is given to do the above task.

Sample Input and Output1:

Enter name: john

Enter age: 64

Enter Gender: m

Enter Contact no: 6547891230

Registered Successfully

Sample Input and Output2:

Enter name: john

Enter age: ten

Invalid Input

Sample Input and Output3:

Enter name: john

Enter age: 64

Enter Gender: 77

Invalid Input

Code:

Main.java

import java.util.*;



public class Main {
    static String name;
    static int age;
    static char gender;
    static long contact;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		
		Marathon m=new Marathon();
		while(true)
		{
		System.out.println("Enter name: ");
	    try{
	        name=sc.next();
	        if(!name.matches("^[a-zA-Z]*$"))
	        {
	            throw new InputMismatchException();
	        }
	    }
	    catch(InputMismatchException i)
	    {
	        System.out.println("Invalid Input");
	        break;
	    }
		System.out.println("Enter age: ");
		try{
		    age=sc.nextInt();
		}
		catch(InputMismatchException i)
		{
		    System.out.println("Invalid Input");
		    break;
		}
		
		
	
		System.out.println("Enter Gender: ");
	    try{
	        gender=sc.next().charAt(0);
	        if(!(gender=='m' || gender=='f' || gender=='M' || gender=='F'))
	        {
	            throw new InputMismatchException();
	        }
	    }
	    catch(InputMismatchException i)
	    {
	        System.out.println("Invalid Input");
	        break;
	    }
		System.out.println("Enter Contact no: ");
		try{
		    contact=sc.nextLong();
		}
		catch(InputMismatchException i)
		{
		    System.out.println("Invalid Input");
		    break;
		    
		}
		//Fill the code
		m.setName(name);
		m.setAge(age);
		m.setGender(gender);
		m.setContactNo(contact);
		System.out.println("Registered Successfully");
		break;
	}

}
}

Marathon.java

public class Marathon {
	
	private String name;
	private int age;
	private char gender;
	private long contactNo;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	public long getContactNo() {
		return contactNo;
	}
	public void setContactNo(long contactNo) {
		this.contactNo = contactNo;
	}
	
	public Marathon(){}

}

Recommended:

  1. Birthday
  2. Marathon Registration
  3. Array Manipulation – Use try with multi catch

Leave a Comment

Your email address will not be published. Required fields are marked *

x