Help on searching a string array needed!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scoss04
    New Member
    • Jan 2008
    • 2

    #1

    Help on searching a string array needed!

    Please can someone elp me with this peice of code below, i am trying to get the user to enter a an address, search through the string arrary address, if its found then stop if not found then get them to input data for the rest of the arrays. i can get it to work if i pre set values into the address string
    e.g. String [] address = {"rob st","king rd","mary st");
    but i need to get the user to enter the data for that too, any help would be great

    Code:
    import java.io.*;
    
    public class test4
    {
      public static void main(String[] args) throws IOException
      {
        
          final int maxStudents = 10;
        String [] address = new String [maxStudents];
          String [] firstname = new String [maxStudents];
          String[] lastname = new String [maxStudents];
          String [] telno = new String [maxStudents];
          int activeEntries = 0;
            boolean found = false;
        int i = 0;
          
        String addressnosearch = Text.readString("Input name to search for, * to finish");
        
        while ( !addressnosearch.equals("*") )
        {
          found = false;
          i = 0;
         
           
    
         
          while ( !found && i < address.length)
          {
            
            if (address[i].equals(addressnosearch) )  //could use == true
            {
              found = true;
            }
            else
            {
              i++;
            }//end-if
          }//end-while
          
          if (found == true)
          {
            Text.showMessage ( addressnosearch + " was found in the array");
          }
          else
          {
              address[activeEntries] = addressnosearch;
              firstname[activeEntries] =  Text.readString("Enter First Name " );
              lastname[activeEntries] =  Text.readString("Enter Last Name " );
              telno[activeEntries] =  Text.readString("Enter Telephone no " );
              activeEntries = activeEntries + 1;
          }//end-if
             }//end-while
    
        System.exit(0);
    
      }//main
    
    }//LinearSearch


    i get this message after entering an address for
    String addressnosearch = Text.readString ("Input name to search for, * to finish");

    Exception in thread "main" java.lang.NullP ointerException
    at test4.main(test 4.java:31)
    Process exited with exit code 1.
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    What is "Text"?

    Also this code shows the anti-pattern "object denial":
    [CODE=Java]
    String [] address = new String [maxStudents];
    String [] firstname = new String [maxStudents];
    String[] lastname = new String [maxStudents];
    String [] telno = new String [maxStudents];[/CODE]
    Why not define an Addess class?

    Comment

    • bigbrother
      New Member
      • Jan 2008
      • 3

      #3
      in line 30:

      if (address[i].equals(address nosearch) ) //could use == true

      the variable address[i] is equals to null like all the others locations in the array
      address.
      to avoid this you can write another if like this

      if(address[i]==null) //will not work with .equals!!!
      i++;
      else if (address[i].equals(address nosearch) )
      found = true;

      else
      i++;
      .
      .

      hope i've helped
      orev hara

      Comment

      Working...