• 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

    • I've had similar thoughts about the 2TB one. The price came down to a more tempting level a few months ago, but juggling installs with a far cheaper USB SSD hasn't been so bad. Even big games rip around pretty quick when you can push them at 4gbps, and that drive can be repurposed when its time with the Xbox is done. I'd need an iron-clad promise that it'll be 100% feature complete, next-gen compatible to even consider a 4TB unit.
    • It's not. It's a rightwing lie peddled by liars to BillyBob'sFreedumb email chain. Feel free to provide any credible source to support this claim. You won't find a single one. The reason Biden wasn't prosecuted was because it was a handful of documents from the Obama years that were just in storage. And when the FBI asked for them, Biden and his team did everything they could to find them and return them immediately -- even ones the FBI didn't know were "missing". https://en.wikipedia.org/wiki/...assified_documents_incident Whereas the Charlatan in Chief stored mountains of classified material out in the open in his pay to play club infested with foreign spies. https://www.pbs.org/newshour/p...-mar-a-lago-shower-ballroom Trump also revealed this nation's most classified information to foreigners who had no clearance to access them...and then bragged about knowing them to their friends overseas. https://www.nbcnews.com/politi...arines-according-rcna119173 And not only are many still missing and not returned... https://www.cnn.com/interactiv...ssia-intelligence-trump-dg/ Trump denied having them to the FBI repeatedly and moved them around to avoid having to return them to the proper authorities for over a year. https://www.pbs.org/newshour/p...ing-of-classified-documents There is no credible legitimate comparison between the two classified documents cases. You might want to change the source of the information you are getting and falling for. They are quite obviously lying to you. PS On the content issue...I am a content creator. You and everyone else in the world loves the franchises and content I have created and/or contributed meaningfully to. It's the studios that are doing everything they can to remove us, the content creators, from their balance sheets (now with AI)...not the people who consume what we create for free. We've already been paid, thanks. Blame Wall Street for forcing the bottomless greedy enshittification of everything American, not the consumers, pirates, or the actual creators.
    • https://support.microsoft.com/...61ff-00a1-04e2-2d1f3865450d
    • https://support.microsoft.com/...61ff-00a1-04e2-2d1f3865450d
  • Recent Achievements

    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
    • First Post
      ClarkB earned a badge
      First Post
    • Week One Done
      Epaminombas earned a badge
      Week One Done
    • Week One Done
      Prestige Podiatry Care earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      144
    2. 2
      Xenon
      127
    3. 3
      ATLien_0
      124
    4. 4
      +Edouard
      102
    5. 5
      snowy owl
      98
  • Tell a friend

    Love Neowin? Tell a friend!