• 0

How to get the name of the oldest person with java array and split string?


Question

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

2 answers to this question

Recommended Posts

  • 1

I assume the problem is that it's always printing the last name you entered.
Here's a hint, there's an issue with where you've declared the "oldest" variable. 

  • 0

The issue I always have with things like this is I end up creating code that gets the job done, but is probably not what they're looking for at this stage. I'd personally just create a Person class and parse each string into its own instance of the class, and then compare it that way lol. It's a solution that totally works (and is also expandable), but it's a more advanced solution that wouldn't really be considered or taught at this point of such a course.

 

Anyway, sometimes it helps to just "run" the code by hand. Use a pencil and paper and just go line by line as if you were the computer, following the loops (like the while (true) loop you have) and writing variables and how they change as you go along. For logic questions like this, it can be a nice strategy to use. Actual programmers do a similar thing, it's called "stepping through code" - it's just done using a computer tool called a debugger rather than using a pencil and paper.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Posts

    • RIP Hotlips..... IMHO, her best scenes were the few where she dared to let her REAL feminine side show through from underneath all that crappy, worn-torn soldier facade that she had to keep up with...she instantly lit up every room or situation where she was featured !
    • Helium Converter 3.3.69.0 by Razvan Serea Helium Converter is a free Windows utility for converting audio files between formats such as MP3, FLAC, AAC, WMA, OGG, and WAV. It supports batch conversion, preserves or updates tag information, and offers features like volume normalization. With a simple interface, it's ideal for users who need to convert large music libraries quickly and efficiently while retaining metadata. Helium Converter key features: Supports file formats: MP3, MP4, FLAC, AAC, M4A, WMA, WAV, OGG, OPUS, APE.... Batch conversion for large music libraries Preserves and edits metadata (ID3, Vorbis Comments, etc.) Volume normalization to equalize loudness Album art extraction and embedding Drag-and-drop interface for quick file selection Adjustable encoding parameters (bitrate, sample rate, channels) Uses internal codecs for consistent performance Supports CUE sheets for split track conversion File renaming based on tags during export Unicode support for international file and tag names Logging of conversion processes for troubleshooting Multi-core CPU support for faster conversions Download: Helium Converter 3.3.69.0 | 25.1 MB (Freeware) Links: Helium Converter Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Since it's been quite a while since the last episode aired, would it be fair (or cruel) to refer to Peggy as a MILF, or even a SMILF ?  HAHAHAHAHAHA
    • Their computers are not gonna stop working in October
    • Crowdstrike the same service provider that caused millions of in damages? I hate disliking a company for a singular failure but they really screwed up.
  • Recent Achievements

    • One Year In
      WaynesWorld earned a badge
      One Year In
    • First Post
      chriskinney317 earned a badge
      First Post
    • Week One Done
      Nullun earned a badge
      Week One Done
    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      173
    2. 2
      ATLien_0
      125
    3. 3
      snowy owl
      122
    4. 4
      Xenon
      116
    5. 5
      +Edouard
      93
  • Tell a friend

    Love Neowin? Tell a friend!