Discussion of Activity 2.5 - Using arrays

Here is an example solution selectAPlanet method. We have commented the places where we have made changes.

public void selectAPlanet()
{
   // We modified the array to add Pluto in ninth position from the Sun
   // (Its index in the array is 8, which is planets.length)

   String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter",
                       "Saturn", "Uranus", "Neptune","Pluto"};

   System.out.println("THE SOLAR SYSTEM");
   Scanner scn = new Scanner(System.in);

   /* notice that the following statement still works,
    * because it did not assume a particular length of the planets array
    */
   System.out.println("Which number planet do you choose(1 - " + planets.length + ")?");

   System.out.println("Type your choice and press ENTER.");

   int choice = scn.nextInt();

   // We added a check that the choice was not beyond the length of the array
   if (choice < 1 || choice > planets.length)
   {
      System.out.println("Sorry, there is no such planet.");
   }
   else
   /* We concatenated the name of the planet to the output string,
    * adjusting the choice number to the index position of the chosen
    * planet in the array
    */
   {
      System.out.println("Your chosen planet was "  + planets[choice - 1] + ".");
   }
}