In this task, you are being asked to write void methods in Java.
Write a void method called getInput() that takes input using a Scanner object, the name and age
from a user. This method calls another method called printInput() that takes two arguments, one
String (for the user name) and another int (for the user age) and prints both values using
appropriate messages.
You may create method headers as:
static void getInput()
and
static void printInput(String name, int age)
NOTE: You need to call the printInput() method from the getInput() and pass appropriate
values. The main() method will only call the getInput() method. Also, perform input validation
on the age argument, so that the method should only be called when the age is at least 10 and less
than 70.
1. Create a program called InputMethodLab1.java.
2. Use a Scanner object for the input.
3. Correctly call methods and display appropriate messages.
import java.util.Scanner;
public class InputMethodLab1{
public static void main(String []args){
getInput();
}
static void getInput(){
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter name: ");
String name = keyBoard.nextLine();
System.out.print("Enter age: ");
int age = keyBoard.nextInt();
printInput(name, age);
keyBoard.close();
}
static void printInput(String name, int age){
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Comments
Leave a comment