• 0

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


 Share

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("Name of the oldest: " + name);
    }
}
 

Edited by bimyou
Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By News Staff
      Lifetime membership to Whizlabs Online Certifications, now 94% off
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where you can save 94% off a lifetime membership to Whizlabs Online Certifications. Get lifetime access to affordable world-class certification training courses and gain new, essential industry skills.



      Are you a practicing professional but wanting to learn more? Learn new skills and knowledge at your own pace with Whizlabs. Whizlabs is a pioneer among online training providers across the world. They provide online certification training in various disciplines such as Cloud Computing, Java, Big Data, Project Management, Agile, Linux, CCNA, and Digital Marketing. Launched in 2000, Whizlabs has helped more than 3 million professionals and 100+ companies across the world to succeed in their careers with multitudes of courses. If you want to boost your career or grow in your current field, then sign up to Whizlabs now!

      Access various courses on AWS, Microsoft, Google Cloud, Java, Linux & more Get certifications & validate and demonstrate your new skills Learn from subject-matter experts & certified professionals Get regularly updated content

      Here's the deal:
      Lifetime membership to Whizlabs Online Certifications normally costs $4,499, but you can pick it up for just $239.99 for a limited time - that represents a saving of $4,259.01 (94%) off. For a full description, specs, and instructor info, click the link below.

      Get a lifetime membership to Whizlabs for just $239.99
      C-MAX Chrono-Max Smartwatch
      12 Functions in 1 Device! Control Your Music, Receive Calls, See the Weather, & Track Your Sleep, Fitness, and Health Vitals All on Your Wrist

      Get this deal for just $59.99 (list price $219) 72% off

      Not for you?
      That's OK, there are other deals on offer you can check out here.

      Ivacy VPN - 5 years at 87% off NordVPN - 2 years at up to 68% off Private Internet Access VPN - subscriptions at up to 79% off Unlocator VPN or SmartDNS - unblock Geoblock with 7-day free trial Subscribe to Neowin - for $14 a year, or $28 a year for Ad-Free experience Giveaways: Apple Giveaway | Gaming Giveaway | Amazon Giveaway Neowin Deals · Free eBooks · Neowin Store

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.

    • By News Staff
      Go from Beginner to Master with this Complete Java Developer course
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store where you can save 90% off this Complete Java Developer Course: From Beginner to Master. This is your 9 hours of instruction to mastering the fundamentals of java programming — taught by top-rated instructor Rob Percival.



      Java is 25+ years old and used everywhere! Over 7.6 million developers worldwide use Java to create websites, build apps, and write programs - making it one of the most popular programming languages around. Why is Java so widely used? Well, it's an all-purpose language that can run on any platform, it's quick, easy to learn, highly versatile, and there's a huge community of support online as well. This Java course is aimed at beginners and is designed to get students coding in Java as quickly and confidently as possible. There are practical, hands-on coding challenges in every lecture, so you can practice the code straight away and make sure you've mastered every line!

      Access 61 lectures & 9 hours of content 24/7 Master the fundamentals of Java programming Learn all the tips, tricks & theory behind the code Build exciting portfolio projects Put your new skills to work straight away Here's the deal:
      This Complete Java Developer Course normally costs $200 but it can be yours for just $19.99 for a limited time, that's a saving of $179.01 (90%) off the price. For a full description, specs, and instructor info please click the link below.

      Get the Complete Java Developer Course for $19.99
      Save 98% off the Amazon Side Hustle Bundle
      15 Courses with Massive 108 Hours of Power Content from Top Amazon FBA Instructors — Launch Your Online Business & Earn a Passive Income

      Get the 2021 Amazon Side Hustle Bundle for $24.99 (list price $1,791)

      Not for you?
      That's OK, there are other deals on offer you can check out here.

      Ivacy VPN - 5 years at 87% off NordVPN - 2 years at up to 68% off Private Internet Access VPN - subscriptions at up to 79% off Unlocator VPN or SmartDNS - unblock Geoblock with 7-day free trial Subscribe to Neowin - for $14 a year, or $28 a year for Ad-Free experience Giveaways: Apple Giveaway | Gaming Giveaway | Amazon Giveaway Neowin Deals · Free eBooks · Neowin Store

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.

    • By News Staff
      Mastering the Fundamentals of Java Programming — now 90% off
      by Steven Parker

      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store where you can save 90% off this Complete Java Developer Course: From Beginner to Master. This is your 9 hours of instruction to mastering the fundamentals of java programming — taught by top-rated instructor Rob Percival.



      Java is 25+ years old and used everywhere! Over 7.6 million developers worldwide use Java to create websites, build apps, and write programs - making it one of the most popular programming languages around. Why is Java so widely used? Well, it's an all-purpose language that can run on any platform, it's quick, easy to learn, highly versatile, and there's a huge community of support online as well. This Java course is aimed at beginners and is designed to get students coding in Java as quickly and confidently as possible. There are practical, hands-on coding challenges in every lecture, so you can practice the code straight away and make sure you've mastered every line!

      Access 61 lectures & 9 hours of content 24/7 Master the fundamentals of Java programming Learn all the tips, tricks & theory behind the code Build exciting portfolio projects Put your new skills to work straight away What's the benefit?
      This Complete Java Developer Course: From Beginner to Master normally costs $200 but it can be yours for just $19.99, a saving of $180.01. For a full description, specs, and instructor info please click the link below.

      Get this 2021 Complete Java Developer Course for $19.99
      Not for you?
      That's OK, there are other deals on offer you can check out here:

      Ivacy VPN - 5 years at 87% off NordVPN - 2 years at up to 68% off Private Internet Access VPN - subscriptions at up to 79% off Unlocator VPN or SmartDNS - unblock Geoblock with 7-day free trial Subscribe to Neowin - for $14 a year, or $28 a year for Ad-Free experience Giveaways: Polycade Home Arcade | $5K in cash | $10K in Crypto | Gaming Bundle Neowin Deals · Free eBooks · Neowin Store

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.

    • By News Staff
      Pay What You Want for the 20-Course Programming Bundle
      by Steven Parker



      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where you can Pay What You Want for the The 20-Course Programming Bundle. From game development to making responsive apps, this 20-course collection has everything you need to start coding.

      Beat the average price to get the following courses:

      Azure Deployment for Node.js Applications
      Discover How to Deploy Your Node.js Apps to Microsoft Azure & Make Them Accessible Online

      MongoDB for Beginners
      Get Acquainted with MongoDB & Discover How to Manage Data Operations Like a Pro

      Node.js For Beginners: Create Server-Side Apps with JavaScript
      Learn How to Build Fast Web Applications with Node.js, the Server-Side Solution for JavaScript

      Intro to Next.js
      Get started with the Minimalist Framework That Allows You to Build Static, Server-Rendered React Apps

      Intro to Bootstrap
      Learn Bootstrap, the Secret to Quickly Creating Professional & Attractive Websites + Web Apps

      Create Interactive Pages with Javascript & the DOM API
      Explore the DOM API, HTML5 & Advanced JavaScript Concepts by Building Three Projects

      JavaScript Programming: Learn by Making a Mobile Game
      Dive into JavaScript's Core Concepts By Building a Game From Scratch

      HTML & CSS Course
      Learn HTML & CSS from Scratch by Creating a Responsive Landing Page

      AWS Deployment for Node.js Applications
      Discover How to Deploy Your Node.js Apps to AWS & Make Them Accessible Online

      The Complete HTML5 Mobile Game Development Course
      Use the Phaser HTML5 Framework & JavaScript Technologies to Build Cross-Platform Games

      Master Unity Game Development: Beginner's Bootcamp
      Learn Unity & C# by Creating a 3D Multi-Level Platformer Game

      Master Unity Mobile Game Development
      Learn the Quirks of Mobile Game Development with Unity

      Unity Game Development: Build a First-Person Shooter
      Master Unity by Building Your Own Version of Doom or Call of Duty

      Data Manipulation with Pandas
      Learn How to Read & Manipulate Data + Prepare It for Analysis Using the Pandas Library

      The Complete Python Data Visualization Course
      Master the Major Plotting Libraries Matplotlib, Seaborn & Bokeh + Use Them to Create Beautiful Plots

      Learn Python 3 by Making a Game
      Get up to Speed with Python by Creating Your Very Own Game

      Build a Tower Defense Game with Phaser 3
      Master the Skills Needed to Build a Complete Tower Defense Game Using Phaser 3

      Numpy Matrices & Vectors
      Take Advantage of the Numpy Library & Build Powerful + Functional Arrays and Matrices

      The Complete Responsive Web Design Course
      Learn How to Build Professional Websites That Work Across Different Devices & Platforms

      Pay What You Want (as little as $1) for the following course:

      Image Processing with Python: Build an Instagram-Style Filter
      Develop a Photo Filter Editor from Scratch & Master Your Understanding of Matrices, Color Models & More

      Here's the deal:
      The bundle represents an overall retail value of $3,732 To unlock the full bundle is under $16 at the time of writing. Beat the average price and you'll take home the entire bundle Beat the Leader's price and get entered into the epic giveaway. Pay What You Want for this 20-Course Programming Bundle
      Not for you?
      That's OK, there are other deals on offer you can check out here.

      Ivacy VPN - 5 years at 87% off NordVPN - 2 years at up to 68% off Private Internet Access VPN - subscriptions at up to 79% off Unlocator VPN or SmartDNS - unblock Geoblock with 7-day free trial Neowin Store for our preferred partners. Subscribe to Neowin - for $14 a year, or $28 a year for Ad-Free experience Giveaways: Polycade Home Arcade | $5K in cash | $10K in Crypto | Gaming Bundle Neowin Deals · Free eBooks · Neowin Store

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.


    • By News Staff
      Save 98% off this 27 Course Premium Learn to Code Certification Bundle
      by Steven Parker



      Today's highlighted deal comes via our Online Courses section of the Neowin Deals store, where for only a limited time, you can save 98% off this Premium Learn to Code 2021 Certification Bundle. Amp up your programming skill set with 270+ hours of content on the leading coding languages — taught by top-rated instructors Ft. Rob Percival, Nick Walter, and more!

      This deal consists of the following courses:

      JavaScript Beginner Bootcamp
      Understand the JavaScript Language & Learn to Create and Launch Interactive Webpages SwiftUI Apps for All Apple Platforms
      Learn the True Magic of SwiftUI & Make Apps and Widgets for iPhone, Mac, iPad, Apple Watch, and AppleTV The Complete C# Programming Course
      Master C# & .NET Framework with Code Challenges, Exercises, & Real-Life Examples The Complete Web Developer Course 2.0
      Learn By Doing: Build 25 Websites & Mobile Apps Alongside Top-Rated Online Coding Instructor, Rob Percival! SwiftUI: The Complete Developer Course
      Learn Everything You Need to Know About the SwiftUI Framework & Leverage All of Its Great New Technologies 2020 Complete Ruby on Rails 6 Bootcamp
      Start From the Basics of Ruby on Rails & Go All the Way to Creating Your Own Applications Learn Python for Data Analysis & Visualization
      Gear Yourself with Necessary Skills for Expert-Proven Data Science Techniques Raspberry Pi Essentials & Extras
      Broaden Your Raspberry Pi's Horizons with This 1-Hour Crash Course PHP & MySQL: Certification Course for Beginners
      Learn to Build Database-Driven Web Applications Using PHP & MySQL Bootstrap & jQuery: Certification Course for Beginners
      Create Fully Animated, Interactive, Mobile Responsive Web Pages Using Bootstrap & jQuery Library HTML, CSS, & JavaScript: Certification Course for Beginners
      A Comprehensive Guide for Beginners Interested in Learning HTML, CSS, & JavaScript to Build Interactive Web Pages Java Foundations
      Master the General Purpose & Object-Oriented Language of Java to Enhance Your Skills for Mobile Development Making APIs with Django & Python
      Use the REST Framework to Quickly Make APIs for Your Django Projects Making Websites with Python & Django 3: The Ultimate Beginner's Guide
      Boot Up the Django 3 Web Framework Written In Python & Develop Websites Fast JavaScript Master Class
      A Comprehensive Course Just About Every Fact on JavaScript You Could Ever Hope to Know Python for Everyone
      Learn the Basic Syntax of How Python Language Works How to Get a Job as a Web Developer
      Tips, Tricks & Industry Secrets to Help You Land That Web Development Job The Python Mega Course: Build 10 Real World Applications
      A Complete Practical Python Course for Beginners & Intermediates! Master Python 3 by Building Actual Python Apps ROS2 For Beginners
      Master the Key ROS Concepts to Create Powerful & Scalable Robot Applications The Complete Web Developer Course: Build 20 Websites
      Learn the Fundamentals of HTML, CSS & JavaScript by Building 20 Real-World Websites Machine Learning for Absolute Beginners
      Learn to Build Machine Learning Models Using Spreadsheet Data Java Interview Questions: Data Structures & Algorithms
      The Best Java Interview Guide with the Most Common Beginner Questions Asked by FANG Companies

      The Ultimate Amazon Honeycode Guide: Build 7 Apps
      Build Apps to Automate Data Entry: Make Inventory Management Apps & More Easy Business Projects

      Learn Flutter & Dart: Complete App Development Projects
      Build Apps in Flutter, Google's Cross-Platform Framework & Code with Its Dart Programming Language

      Complete Data Science Training with Python for Data Analysis
      Learn Statistics, Visualization, Machine Learning & More

      C++ Programming for Beginners
      Master C++, An In-Demand, High-End Language Used for Programs, Games, Development Libraries, & More

      The Complete Front-End Web Development Course
      Take a Hands-on Approach to Learning Real Programming Skills

      Here's the deal:
      This Premium Learn to Code 2021 Certification Bundle normally costs* $4,056, but you can pick it up for just $59.99 for a limited time - that represents a saving of $3,996.01 (98%) off. For a full description, specs, and instructor info click the link below.

      Get the Premium Learn to Code 2021 Certification Bundle
      Not for you?
      That's OK, there are other deals on offer you can check out here.

      Ivacy VPN - 5 years at 87% off NordVPN - 2 years at up to 68% off Private Internet Access VPN - subscriptions at up to 79% off Unlocator VPN or SmartDNS - unblock Geoblock with 7-day free trial Neowin Store for our preferred partners. Subscribe to Neowin - for $14 a year, or $28 a year for Ad-Free experience Giveaways: Polycade Home Arcade | $5K in cash | $10K in Crypto | Gaming Bundle Neowin Deals · Free eBooks · Neowin Store

      Disclosure: This is a StackCommerce deal or giveaway in partnership with Neowin; an account at StackCommerce is required to participate in any deals or giveaways. For a full description of StackCommerce's privacy guidelines, go here. Neowin benefits from shared revenue of each sale made through our branded deals site, and it all goes toward the running costs.