Advertisement
Vendrick-Xander

LongestNames

Jan 30th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. Longest Name Object Class:
  2. package com.Suarez;
  3. /*
  4. Xander Fermier
  5. 1/30/19
  6. this object class creates method to use longest names idea of finding longest name the user inputs
  7. */
  8. import java.util.*;
  9.  
  10. public class LongestName {
  11. public void LongestName(String console, int numOfNames)
  12. {
  13. if (console.equals("Console") || console.equals("console"))
  14. {
  15. Scanner scan = new Scanner(System.in);
  16. StringBuilder sb = new StringBuilder();
  17. String longestName = "";
  18. int inputLength = 0;
  19. for (int i = 0; i <numOfNames; i++)
  20. {
  21. System.out.println("Name #" + (i + 1) + "?");
  22. String userInput = scan.nextLine();
  23. inputLength = userInput.length();
  24. int longestNameLength = longestName.length();
  25. if (inputLength > longestNameLength)
  26. {
  27. longestName = userInput;
  28. }
  29. }
  30. char firstChar = longestName.charAt(0);
  31. String properFirstChar1 = String.valueOf(firstChar);
  32. String properFirstChar2 = properFirstChar1.toUpperCase();
  33. String secondPart = longestName.substring(1,(inputLength));
  34. sb.append(properFirstChar2);
  35. sb.append(secondPart);
  36. System.out.println("The longest name is " + sb);
  37. }
  38. }
  39. }
  40. Longest Name Client Class:
  41. package com.Suarez;
  42. /*
  43. Xander Fermier
  44. 1/30/19
  45. client class to call the longestnames method
  46. */
  47. public class LongestNameClient {
  48. public static void main(String[]args)
  49. {
  50. LongestName names = new LongestName();
  51. names.LongestName("console", 4);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement