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

Maximum value and its index in an array java program that: Receive as input in command...
Maximum value and its index in an array java program that: Receive as input in command line the size of an array (n) Generate an array with numbers between 1 and 100 (inclusive 1 and 100) with the given size (a) Print the array with the format: {a, b, c, ..., x}, for instance {1, 2} Compute and print in the console the following: maximum value in the array one index on which the maximum value appears (a number from...
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...
Submit a java file. Write a program where you input 4 numbers and the output generates...
Submit a java file. Write a program where you input 4 numbers and the output generates the average of the 4 numbers. The output may not be negative and <= 100. If any value is negative or >100, it should be replaced by the value of 30. For all such values, you will replace them with a value of 10. The program should produce the following output: Today's average amount is: $xx.xx.
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.
Program using java Take user input and give corresponding output. A user is considering different options...
Program using java Take user input and give corresponding output. A user is considering different options of operating air conditioning. Depending on room temperature (here, room temperature is given by user), this program should give different instructions. There are three scenarios. - If temperature is above 90, the program should output “cooling”. -If the temperature is below 70, the program should output “heating”. -Otherwise, the program should output “stopped”. For example, if user enters “95”, this is how the program...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT