2 Replies Latest reply: Nov 7, 2006 12:49 PM by 807599 RSS

    Compare string variable to String inside array??

    807599
      I need to have a program that checks the names in the array and if it finds a match to what has been entered it prints out the position of the name in the array.
      import java.util.Scanner;
      
       public class ArrayTwo
       {
           public static void main(String args[])
           {
           
           Scanner keyboard = new Scanner(System.in);
           
           String[] B = {"Halifax", "Enfield", "Elmsdale", "Truro", "Springfield", "Sackville", "Moncton"};
      
           
            System.out.println("Please enter the name of a town ");
            String townName = keyboard.next();
            
            for (int index = 0; index < B.length; index++) 
            {
                 if(townName.equals(B[index))
            }
                
                }
                
         }
      }
      I kno the .equals will not work for this. Any ideas?