Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Arrays Gotchas and Wins Array Usage in Method Declarations

Error Creating the pickLunchSpot Function

Hi, I can not recreate the pickLunchSpot function, that Craig demonstrated in JShell. "Illegal start of expression error keeps coming up. Can someone help me out?

import java.util.Random;

public class RandomlyPickLunchSpot {

    public static void main(String[] args) {

        // method takes varargs: an indefinite number of parameters of type String
        // and turns them into an array
        // randomly pick one item of the array
        public static String pickLunchSpot(String... spots) {
            System.out.printf("Randomly picking %d lunch spots. %n",
                        spots.length);
            Random random = new Random();
            return spots[random.nextInt(spots.length)];
        }
        String lunchSpot = pickLunchSpot("Que Sabrosa", "Las Primas", "Life of Pie");
        System.out.printf("%s %n", lunchSpot);
    }

}

The following error comes up:

RandomlyPickLunchSpot.java:10: error: illegal start of expression
        public static String pickLunchSpot(String... spots) {
        ^
RandomlyPickLunchSpot.java:17: error: <identifier> expected
        System.out.printf("%s %n", lunchSpot);
                         ^
RandomlyPickLunchSpot.java:17: error: illegal start of type
        System.out.printf("%s %n", lunchSpot);
                          ^
RandomlyPickLunchSpot.java:20: error: class, interface, or enum expected
}
^
4 errors

1 Answer

Brendon Butler
Brendon Butler
4,242 Points

The problem is that you can't encapsulate one java method within another. Your pickLunchSpot method is inside your main method. Instead, you need to close your main method, and put the pickLunchSpot method outside of the braces. Like this:

RandomlyPickLunchSpot.java
import java.util.Random;

public class RandomlyPickLunchSpot {

  public static void main(String[] args) {
    String lunchSpot = pickLunchSpot("Que Sabrosa", "Las Primas", "Life of Pie");
    System.out.printf("%s %n", lunchSpot);
  } /* close main method here */

  /* start pickLunchSpot method here (EDIT: removed default comments for visual clarity) */
  public static String pickLunchSpot(String... spots) {
    System.out.printf("Randomly picking %d lunch spots. %n",
                spots.length);
    Random random = new Random();
    return spots[random.nextInt(spots.length)];
  }
}