Advertisement
StefanTobler

Lesson 29 Activity 1

Nov 4th, 2016
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. /*
  2.  * Lesson 29 Coding Activity 1
  3.  * A student wants an algorithm to find the hardest spelling
  4.  * word in a list of vocabulary. They define hardest by the longest word.
  5.  * Write the code to find the longest word stored in an array of Strings called list.
  6.  * If several words have the same length it should print the first word
  7.  * in list with the longest length.
  8.  * For example, if the following list were declared:
  9.  *
  10.  *     String list [] = {"high", "every", "nearing", "checking", "food ",
  11.  *     "stand", "value",  "best", "energy", "add", "grand", "notation",
  12.  *     "abducted", "food ", "stand"};
  13.  *
  14.  * It would print:
  15.  *     checking
  16.  */
  17.  
  18. import java.util.Scanner;
  19. import java.lang.Math;
  20.  
  21. class Lesson_29_Activity_One {
  22.  
  23.   public static String [] list = {"high", "every", "nearing", "checking", "food ",
  24.       "stand", "value",  "best", "energy", "add", "grand", "notation",
  25.       "abducted", "food ", "stand"};
  26.  
  27.     public static void main(String[] args)
  28.      {
  29. int s = 0;
  30. for (int i=0; i < list.length; i++)
  31. {
  32. if ( list[s].length() < list[i].length() )
  33.         s = i;
  34. }
  35. System.out.println(list[s]);
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement