how to use/return "public static String [] list={};" (program from amplify unit 3 lesson 29)

Question:

"A student wants an algorithm to find the hardest spelling word in a list of vocabulary. They define hardest by the longest word.

Write the code to find the longest word stored in an array of Strings called list. If several words have the same length it should print the first word in list with the longest length.

For example, if the following list were declared:

String list [] = {"high", "every", "nearing", "checking", "food ", "stand", "value", "best", "energy", "add", "grand", "notation", "abducted", "food ", "stand"};

It would print:

checking

"

My solution:

import java.util.Scanner;

import java.lang.Math; 

class Lesson_29_Activity_One {
  public static String [] list={};
  {
    String list [] = {"high", "every", "nearing", "checking", "food ",
      "stand", "value",  "best", "energy", "add", "grand", "notation",
      "abducted", "food ", "stand"};
  }  
  public static void main(String[] args)
  {
      Scanner console = new Scanner (System.in);
      int longestInt= list[0].length();
      String longest= list[0];
      for (int x=0; x< list.length; x++)
      {
        if (list[x].length() > longestInt) 
        {
          longestInt = list[x].length();
          longest = list[x];
        }
      }
      System.out.println (longest);
  }
}

Problem with my solution:

I receive java.lang.ArrayIndexOutOfBoundsException: 0 when I run the program. But when I put the string array under public static void main(String[] args), the entire program works as it should. Unfortunately, the website requires me to input the string array into "public static String [] list={};". I tried removing all my calculations and printing just a random word from the string array. But I get the same result, java.lang.ArrayIndexOutOfBoundsException: # (# is the random position in the string array that I tried to print out). Am I suppose to put some kind of return for public static String [] list={}?

Jon Skeet
people
quotationmark

Look at this part:

public static String [] list={};
{
 String list [] = {"high", "every", "nearing", "checking", "food ",
    "stand", "value",  "best", "energy", "add", "grand", "notation",
    "abducted", "food ", "stand"};
} 

That is two separate parts. First, a declaration of a String[] variable and an initialization of it to an empty array.

public static String[] list = {};

Next, and entirely separately, an initializer block which declares a local variable:

{
   String list [] = {"high", "every", "nearing", "checking", "food ",
      "stand", "value",  "best", "energy", "add", "grand", "notation",
      "abducted", "food ", "stand"};
}

That list variable is not the same as the variable declared earlier. It's local to the initializer. That code will be executed if you ever create an instance of the class, but it's pointless anyway - it serves no purpose.

What you really want is one statement - just the declaration and initialization, but with data:

public static String[] list= { "high", "every", "nearing", "checking", "food ",
    "stand", "value",  "best", "energy", "add", "grand", "notation",
    "abducted", "food ", "stand"};

(Ideally make the field private and final too...)

people

See more on this question at Stackoverflow