string - Printing specific user input in java -


i have design program gets 3 user-inputs in form:

name1   (any number of spaces)  age1 name2   (any number of spaces)  age3 name3   (any number of spaces)  age3 

then print line has highest age (suppose name3 age3 had highest age i'd print line).

my code:

import java.util.*; public class rankage{     public static void main(string[] args){     system.out.println("enter 3 different names , age of each person respectively:");     scanner sc = new scanner(system.in);     string n1 = sc.nextline();     string n2 = sc.nextline();     string n3 = sc.nextline();      } } 

i know how scan user-inputs, don't know how comparison of number within acquired string print specific 1 (also since there can number of spaces seems more complicated me).

you can use split person's age:

string age = "jon       57".split("\\s+")[1]; // contains "57" 

you can use integer.parseint(age) person's age, number.


if need allow user input name spaces, can adjust number in square brackets ([]). example, [2] require user input first name , last name.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -