In: Computer Science
Program:
import java.util.Scanner;
import java.io.*;
class Main {
public static void main(String[] args) {
try{
Scanner sc = new Scanner(System.in); // Scanner class declaration
/* variable declaration*/
char[] name=new char[100];
char[] occupation=new char[100];
char[] hometown=new char[100];
char[] gender=new char[100];
int age,service;
System.out.print("Name: ");
name = sc.nextLine().toCharArray(); // Accept name from user
String str = new String(name); // Convert to string
for (int i = 0; i < str.length(); i++){ // Loop to check existance of the numbers
if (Character.isDigit(str.charAt(i)) == true){ // check each character existance of the number
System.out.println("Please enter character for Name!!");
System.exit(0); // exit from the program
}
}
System.out.print("Occupation: ");
occupation = sc.nextLine().toCharArray(); // Accept occupation from user
String str1 = new String(occupation); // Convert to string
for (int i = 0; i < str1.length(); i++){// Loop to check existance of the numbers
if (Character.isDigit(str1.charAt(i)) == true){ // check each character existance of the number
System.out.println("Please enter character for Occupation!!");
System.exit(0); // exit from the program
}
}
System.out.print("Age: ");
age = Integer.parseInt(sc.next()); // Accept age from user
sc.nextLine(); // Accept enter
System.out.print("Hometown: ");
hometown = sc.nextLine().toCharArray(); // Accept hometown from user
String str2 = new String(hometown); // Convert to string
for (int i = 0; i < str2.length(); i++){// Loop to check existance of the numbers
if (Character.isDigit(str2.charAt(i)) == true){ // check each character existance of the number
System.out.println("Please enter character for Hometown!!");
System.exit(0); // exit from the program
}
}
System.out.print("Years of Service: ");
service = Integer.parseInt(sc.next()); // Accept service from user
sc.nextLine(); // Accept enter
System.out.print("Gender: ");
gender = sc.nextLine().toCharArray(); // Accept gender from user
String str3 = new String(gender); // Convert to string
for (int i = 0; i < str3.length(); i++){// Loop to check existance of the numbers
if (Character.isDigit(str3.charAt(i)) == true){ // check each character existance of the number
System.out.println("Please enter character for gender!!");
System.exit(0); // exit from the program
}
}
System.out.println("\n\nName: "+str+"\nOccupation: "+str1+"\nAge: "+age+"\nHometown: "+"\nYears of Service: "+str2+"\nGender: "+str3); // Output the details
}
catch(NumberFormatException e){ // Catch if the input is a character instead of a number
System.out.println("Please enter Number!!");
}
}
}
Output: