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:

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.4: A Case Study: Rectangular To Polar Coordinate Conversion
Problem 9E: (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by...
icon
Related questions
Question

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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();

}

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT