Question

In: Computer Science

java program: Input and output the following details. Your program can only receive the correct input....

java program: Input and output the following details. Your program can only receive the correct input. (For example: a notification will be given if you not enter Char data type for Name)
Name: Ali bin Ahmad
Occupation: Technician
Age: 30
Hometown: Negeri Sembilan
Years of Service: 12
Gender: Male

Solutions

Expert Solution

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:


Related Solutions

Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
Your Java program should perform the following things:Take the input from the user about the...
Your Java program should perform the following things:Take the input from the user about the patient name, weight, birthdate, and height.Calculate Body Mass Index.Display person name and BMI Category.If the BMI Score is less than 18.5, then underweight.If the BMI Score is between 18.5-24.9, then Normal.If the BMI score is between 25 to 29.9, then Overweight.If the BMI score is greater than 29.9, then Obesity.Calculate Insurance Payment Category based on BMI Category.If underweight, then insurance payment category is low.If Normal...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Which of the following can serve as both an input to and an output of the...
Which of the following can serve as both an input to and an output of the acquisition / payment process? Check Purchase Order Both A and B Neither A and B
Create and complete a function M-file that will receive an input, ? , and output the...
Create and complete a function M-file that will receive an input, ? , and output the corresponding conversion to radians within the range of a complete circle. For each 30? increment, the output should be displayed as a string, (i.e. pi/2 , pi/4 ,etc.), and any other degree to be displayed numerically. I'm using Matlab, explanations are appreciated.
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output...
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output screenshots. Please upload this document along with your source files (i.e., the .java files) on Blackboard by the due date. 1. (Display three messages) Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun. 2. (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot...
Write a program to perform the following actions: the language is Java – Open an output...
Write a program to perform the following actions: the language is Java – Open an output file named “Assign14Numbers.txt” – Using a do-while loop, prompt the user for and input a count of the number of random integers to produce, make sure the value is between 35 and 150, inclusive. – After obtaining a good count, use a for-loop to generate the random integers in the range 0 ... 99, inclusive, and output each to the file as it is...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT