VIII. Summarize the names and ages in three-column-format For Example, Tom Doe 30 Lisa Young 27 name Age name Manika Baker Tim Robinson Age 25 25 name Jack Jordan Roger Mayes Age 31 34 Compile your file and fix the syntax errors. Run your program to check the logical errors.
Program has two string variables to store names.
It also has two integer variables to store ages.
System.out.print is used to print the values to the output.
Scanner is used to read the values from the input.
Values are printed to the screen using the width and precision values of the string format.
For example %20.10s, specifies that there are total 20 pads from current position and 10 specifies that there should be 10 characters of the string which must be printed right aligned.
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
String yourName,bestFriendsName; //declaring variables to store names
int yourAge,bestFriendsAge; //declaring variables to store ages
Scanner myobj = new Scanner (System.in);
System.out.print(" Enter a Your Name:");
yourName = myobj.nextLine(); //storing your name
System.out.print(" Enter a Your best friends Name:");
bestFriendsName = myobj.nextLine();//storing friends name
System.out.print(" How old is "+yourName+"?");
yourAge = myobj.nextInt();//storing your age
System.out.print(" How old is "+bestFriendsName+"?");
bestFriendsAge = myobj.nextInt();//storing friends age
System.out.printf("%10.10s %30.30s %30.30s%n", "name", yourName,bestFriendsName);
System.out.printf("%10.10s %30.30s %30.30s%n", "age", yourAge, bestFriendsAge);
}
}
Step by step
Solved in 4 steps with 2 images