The problem asks you to write a code to read the firstName, lastName, age, and weight of a person from the console all in one single line and print them as follow:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Answer in Java below.
The problem asks you to write a code to read the firstName, lastName, age, and weight of a person from the console all in one single line and print them as follow:
Sample input:
John Smith 61 189.12131
Sample output:
First name: John
Last name: Smith
Your age is 61 and your weight is 189.12
- solution.java:
import java.util.*;
import java.lang.*;
import java.io.*;
class ProblemSolution
{
public void solution(){
Scanner input = new Scanner(System.in);
String inputString = input.nextLine().trim();
int i = inputString.indexOf(' ');
int j = inputString.indexOf(' ', i+1);
int k = inputString.indexOf(' ', j+1);
String firstName = inputString.substring(0,i);
String lastName = inputString.substring(i+1, j);
int age = Integer.parseInt(inputString.substring(j+1, k));
double weight = Double.parseDouble(inputString.substring(k+1));
System.out.println("FirstName: " + firstName);
System.out.println("LastName: " + lastName);
System.out.printf("Your age is %d and your weight is %.2f", age, weight);
}
}
class DriverMain
{
public static void main(String args[])
{
ProblemSolution problemSolution = new ProblemSolution();
problemSolution.solution();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images