- 0
How to get the name of the oldest person with java array and split string?
-
Recently Browsing 0 members
- No registered users viewing this page.
-
Similar Content
-
Download: Java All-in-One For Dummies, 7th Edition ($27 Value) — free for your email
By News Staff,
- ebook offer
- sponsored
- (and 3 more)
- 0 replies
- 3 views
-
Learn Java with Projects (worth $44.99) but free in exchange for your email
By News Staff,
- packt
- ebook offer
- (and 2 more)
- 0 replies
- 3 views
-
JavaScript from Frontend to Backend ($20.99 Value) FREE for your work email
By News Staff,
- ebook offer
- sponsored
- (and 3 more)
- 0 replies
- 3 views
-
- 0 replies
- 3 views
-
Job Ready Java ($24 value) free download for a Limited Time
By News Staff,
- ebook offer
- java
- (and 1 more)
- 0 replies
- 2 views
-
Question
bimyou
I've been staring at this problem for too long.. This is an exercise from https://java-programming.mooc.fi/part-3/4-using-strings and I have to get the name of the oldest person through splitting the strings, and finding the name with the oldest age.
Sample Input: Johnny, 5
Rose, 19
Sam, 10
Desired Output: Rose
Any help would genuinely be appreciated. Thanks!
import java.util.Scanner;
public class NameOfTheOldest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = "";
while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
break;
}
String[] pieces = input.split(",");
int age = Integer.valueOf(pieces[1]);
int oldest = 0;
if (age > oldest) {
name = pieces[0];
oldest = age;
}
}
System.out.println("Name of the oldest: " + name);
Edited by bimyou}
}
Link to comment
Share on other sites
2 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now