- 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
-
JetBrains releases IntelliJ IDEA 2025.1.1, bringing multiple bug fixes and improvements
By David Uzondu,
- jetbrains
- intellij idea
- (and 6 more)
- 0 replies
- 0 views
-
- 14 replies
- 0 views
-
High Performance with Java ($33.99 Value) free eBook download
By News Staff,
- ebook offer
- sponsored
- (and 2 more)
- 0 replies
- 4 views
-
- 0 replies
- 8 views
-
Java and Algorithmic Thinking for the Complete Beginner ($9.99 Value) free download
By News Staff,
- ebook offer
- sponsored
- (and 1 more)
- 0 replies
- 4 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("Namehe oldest: " + name);
Edited by bimyou}
}
Link to comment
https://www.neowin.net/forum/topic/1393747-how-to-get-the-name-of-the-oldest-person-with-java-array-and-split-string/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