How can I count the number of characters in a string value, within an array, and then compare it to others to find the longest one?

mightymorphinParkRanger :

Below is what I have right now. The primary thing I just need to know is how I can check the number of characters of a string, within an array, and then check that against all the other entries and only pull the largest one. The question I'm dealing with is:

Write a program that reads names and birth years from the user until an empty line is entered. The name and birth year are separated by a comma.

After that the program prints the longest name and the average of the birth years. If multiple names are equally longest, you can print any of them. You can assume that the user enters at least one person. Sample output

sebastian,2017 lucas,2017 lily,2017 hanna,2014 gabriel,2009

Longest name: sebastian Average of the birth years: 2014.8

import java.util.Scanner;
public class Test112 {

    //Asks the user for input and will print out who is the oldest


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int checkVal = 0;
        String checkName = null;

        System.out.println("Provide a name and age in this format: name,#");

        while (true) {

            //The unique thing about this code, is each input is a separate array that is not put in memory
            //You just use the array system to pull data of the values entered at that specific time
            String userIn = scanner.nextLine();

            if (userIn.equals("")) {
                break;
            } 

            //Here we get the info from the user, then split it
            String[] valArray = userIn.split(",");

            //This now uses the variable to check which value of arguments entered is the greatest
            //It'll then assign that value to the variable we created
            **if (checkVal < Integer.valueOf((int)valArray[0].length) {**
                //We need the checkVal variable to be a constant comparison against new entries
                checkVal = Integer.valueOf(valArray[1]);
                //This pulls the name of the value that satisfies the argument above
                checkName = valArray[0];
            }
        }

        System.out.println("Name of the oldest: " + checkName);
    scanner.close();

    }
}
Arvind Kumar Avinash :

How can I count the number of characters in a string value, within an array, and then compare it to others to find the longest one?

Do it as follows:

if (checkVal < valArray[0].length()) {
    checkVal = valArray[0].length();
    checkName = valArray[0];
}

Note that length() is used to find the length of a String.

Some other important points:

  1. You also need a variable to store the sum of all the birth years so that you can calculate their average. Alternatively, you can calculate the running average.
  2. Trim the entries (name and birth year) before performing any operation on them.
  3. Do not close the Scanner for System.in.

Given below is the complete program incorporating the notes:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int checkVal = 0;
        String checkName = "", name, birthYearStr;
        int sum = 0, count = 0;

        while (true) {
            System.out.print("Provide a name and age in this format: name,#");
            String userIn = scanner.nextLine();

            if (userIn.equals("")) {
                break;
            }
            String[] valArray = userIn.split(",");
            name = valArray[0].trim();
            birthYearStr = valArray[1].trim();
            if (valArray.length == 2 && birthYearStr.length() == 4) { // Birth year should be of length 4
                try {
                    sum += Integer.parseInt(birthYearStr);
                    if (checkVal < name.length()) {
                        checkVal = name.length();
                        checkName = name;
                    }
                    count++;
                } catch (NumberFormatException e) {
                    System.out.println("Birth year should be an integer. Please try again.");
                }
            } else {
                System.out.println("This is an invalid entry. Please try again.");
            }
        }

        System.out.println("Longest name: " + checkName);
        System.out.println("Average of birth years: " + (sum / count));
    }
}

A sample run:

Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily,2017
Provide a name and age in this format: name,#hanna,2014
Provide a name and age in this format: name,#gabriel,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014

Another sample run:

Provide a name and age in this format: name,#sebastian,201
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#sebastian,hello
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily,2017
Provide a name and age in this format: name,#hanna,2014
Provide a name and age in this format: name,#gabriel,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014

Another sample run:

Provide a name and age in this format: name,#hello,2018,sebastian,2017
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#hello,2018
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2017

Another sample run:

Provide a name and age in this format: name,#sebastian,rama
Birth year should be an integer. Please try again.
Provide a name and age in this format: name,#sebastian,12.5
Birth year should be an integer. Please try again.
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#abcdefghi,2018
Provide a name and age in this format: name,#rama,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014

Another sample run:

Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily   ,          2017
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2017

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=355743&siteId=1