Write a Java program NameInput.java with the behavior shown below. The user inpu
ID: 3670006 • Letter: W
Question
Write a Java program NameInput.java with the behavior shown below. The user input should be after the colon.
Enter first name: Sam
Enter last name: Student
Enter age: 18
Enter birth month: September
Enter birth day: 15
Enter birth year: 1997
As an output, you should print “Sam Student is 18 years old and was born on September 15, 1997.” (without the quotes).
I think my teacher wants us to write this code using methods but I'm not sure how to do so using import java.util.Scanner. Thank you in advance.
Explanation / Answer
import java.util.Scanner;
public class NameInput {
public static void main(String[] args) {
String firstName;
String lastName;
int age;
String birthmonth;
int birthday;
int birthyear;
//Create scanner to obtain user input
Scanner scanner1 = new Scanner( System.in );
//obtain user input
System.out.print("Enter your first name: ");
firstName = scanner1.nextLine();
System.out.print("Enter your last name: ");
lastName = scanner1.nextLine();
System.out.println("enter age:");
age=scanner1.nextInt();
System.out.println("Enter your birth month: ");
birthmonth = scanner1.nextLine();
System.out.print("Enter your birth day: ");
birthday = scanner1.nextInt();
System.out.print("Enter your birth year: ");
birthyear = scanner1.nextInt();
//output information
System.out.print(firstName+" "+lastName+"is"+age+"old and he was born on"+birthmonth +" "+birthday +","+birthyear);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.