6 Replies Latest reply on Feb 13, 2008 7:02 PM by 807601

    Accessing Values from String Array

    807601
      Hello, I am trying to make a state and capitol array, then access it based on input of the state and have it print the state in JOptionPane MESSAGE.
      I am having trouble filter out the correct place in the array based on the input so it can print out.... was wondering if anyone could help. Appreciate it. Here is the code I have so far....

      import javax.swing.JOptionPane;
      import java.io.*;


      /*     
      * myArray.java
      */
      public class myArray {
      public myFinal() {
           String stat;
           for( int i=0; i<statesArray.length; ++i ){
                     stat = JOptionPane.showInputDialog("Enter the state which you would like to find the Capitol of. ");
           stat = (statesArray[0]);
                               
                //the problem is right in here********
           
      JOptionPane.showMessageDialog(null, " " +statesArray[i][1] , "Robert T. Deems", JOptionPane.INFORMATION_MESSAGE);
                          System.exit(0);
      }
      }
      public static void main(String[] args) {
      new myArray();
      }
      private String statesArray[][] = {
           {"Alabama","Montgomery"},
      {"Alaska","Juneau"},
      {"Arizona","Phoenix"},
      {"Arkansas","Little Rock"},
      {"California","Sacramento"},
      {"Colorado","Denver"},
      {"Connecticut","Hartford"},
      {"Delaware","Dover"},
      {"Florida","Tallahassee"},
      {"Georgia","Atlanta"},
      {"Hawaii","Honolulu"},
      {"Idaho","Boise"},
      {"Illinois","Springfield"},
      {"Indiana","Indianapolis"},
      {"Iowa","Des Moines"},
      {"Kansas","Topeka"},
      {"Kentucky","Frankfort"},
      {"Louisiana","Baton Rouge"},
      {"Maine","Augusta"},
      {"Maryland","Annapolis"},
      {"Massachusetts","Boston"},
      {"Michigan","Lansing"},
      {"Minnesota","St. Paul"},
      {"Mississippi","Jackson"},
      {"Missouri","Jefferson City"},
      {"Montana","Helena"},
      {"Nebraska","Lincoln"},
      {"Nevada","Carson City"},
      {"New Hampshire","Concord"},
      {"New Jersey","Trenton"},
      {"New Mexico","Santa Fe"},
      {"New York","Albany"},
      {"North Carolina","Raleigh"},
      {"North Dakota","Bismarck"},
      {"Ohio","Columbus"},
      {"Oklahoma","Oklahoma City"},
      {"Oregon","Salem"},
      {"Pennsylvania","Harrisburg"},
      {"Rhode Island","Providence"},
      {"South Carolina","Columbia"},
      {"South Dakota","Pierre"},
      {"Tennessee","Nashville"},
      {"Texas","Austin"},
      {"Utah","Salt Lake City"},
      {"Vermont","Montpelier"},
      {"Virginia","Richmond"},
      {"Washington","Olympia"},
      {"West Virginia","Charleston"},
      {"Wisconsin","Madison"},
      {"Wyoming","Cheyenne"}};
      }
      class printState {
      public printState(String state, String city){
                
                     this.state=state;
           this.city=city;

      }
      public String toString(){
           if (state == (statesArray[i][0]))
      return " The Capital of: "+state+" is: "+city;
      }
      private String state, city;
      }