0

I searched stackoverflow pretty thoroughly and tried a few suggestions from other questions, but none solved my issue. I can't see ANY reason why this shouldn't work. Normally I would say forget it and let the garbage collector deal with it but my instructor requires the scanner be closed every time. Yes, this is a homework assignment.

Everything in the following code works fine, but when I try to close the scanner it gives me an unreachable code error in Eclipse. I've attempted to open the scanner in different places, but that causes runtime errors since it needs the scanner every time the logic runs. When I put input.close(); at the very end of the main method (which works fine in other assignments I've completed), but something isn't working right here. Here is and slightly abridged version of the code:

import java.util.Scanner;

public class stateBird {

public static int stateInformation(String stateBirdFlower[][],String state)
   {
       int arrayPos = -1;
       boolean found = false;
       for (int index = 0; index < stateBirdFlower.length && !found; index++)
       {
           if(stateBirdFlower[index][0].equalsIgnoreCase(state))
               arrayPos=index;            
       }
       return arrayPos;
   }


public static void main(String[] args) {
        Scanner input = new Scanner(System.in);


        String[][] stateInformation = new String[][] {
            {"Alabama", "Yellowhammer", "Camelia"},
            {"Alaska", "Willow Ptarmigan", "Forget-Me-Not"},
            {"Arizona", "Cactus Wren", "Saguaro Cactus Blossom"}
        };

       while(true) {

            System.out.print("Enter the full name of the state you would like to view or type 'none' if you would like to quit: ");

            String stateName = input.nextLine();


            if(stateName.equalsIgnoreCase("None")) {
                System.exit(0);
            }
            else {
                int position = stateInformation(stateInformation, stateName);
                if(position != -1) {

                    System.out.println("\n" + stateName + "'s " + "Bird is: " + stateInformation[position][1]);
                    System.out.println(stateName + "'s " + "Flower is: " + stateInformation[position][2] + "\n" );                 

                }
                else {
                    System.out.println("Invalid State Entered");
                }              



            }   //End if (stateName...) statement  

       } //End while(true) statement

    input.close();   
    }//End main method

}//End class stateBird

As I stated above, everything in the code works fine, so what is going on with my input.close()? It is opened at the very beginning of the main method, and closed at the very end, that has worked every time before for me, what's different here?

Thanks!

bnr32jason
  • 113
  • 1
  • 11
  • The combination of the `while (true)` and `System.exit(0)` mean that it's impossible for `input.close()` to ever be called. In your case, you shouldn't be closing the `System.in` stream anyway – MadProgrammer Sep 29 '15 at 04:20
  • read javadoc for System.exit() method - it's not cleaning up. what you need is if(stateName.equalsIgnoreCase("None")) {break;} – ilj Sep 29 '15 at 04:26

0 Answers0