Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Longest Name Object Class:
- package com.Suarez;
- /*
- Xander Fermier
- 1/30/19
- this object class creates method to use longest names idea of finding longest name the user inputs
- */
- import java.util.*;
- public class LongestName {
- public void LongestName(String console, int numOfNames)
- {
- if (console.equals("Console") || console.equals("console"))
- {
- Scanner scan = new Scanner(System.in);
- StringBuilder sb = new StringBuilder();
- String longestName = "";
- int inputLength = 0;
- for (int i = 0; i <numOfNames; i++)
- {
- System.out.println("Name #" + (i + 1) + "?");
- String userInput = scan.nextLine();
- inputLength = userInput.length();
- int longestNameLength = longestName.length();
- if (inputLength > longestNameLength)
- {
- longestName = userInput;
- }
- }
- char firstChar = longestName.charAt(0);
- String properFirstChar1 = String.valueOf(firstChar);
- String properFirstChar2 = properFirstChar1.toUpperCase();
- String secondPart = longestName.substring(1,(inputLength));
- sb.append(properFirstChar2);
- sb.append(secondPart);
- System.out.println("The longest name is " + sb);
- }
- }
- }
- Longest Name Client Class:
- package com.Suarez;
- /*
- Xander Fermier
- 1/30/19
- client class to call the longestnames method
- */
- public class LongestNameClient {
- public static void main(String[]args)
- {
- LongestName names = new LongestName();
- names.LongestName("console", 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement