473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class test4
  4. {
  5.   public static void main(String[] args) throws IOException
  6.   {
  7.  
  8.       final int maxStudents = 10;
  9.     String [] address = new String [maxStudents];
  10.       String [] firstname = new String [maxStudents];
  11.       String[] lastname = new String [maxStudents];
  12.       String [] telno = new String [maxStudents];
  13.       int activeEntries = 0;
  14.         boolean found = false;
  15.     int i = 0;
  16.  
  17.     String addressnosearch = Text.readString("Input name to search for, * to finish");
  18.  
  19.     while ( !addressnosearch.equals("*") )
  20.     {
  21.       found = false;
  22.       i = 0;
  23.  
  24.  
  25.  
  26.  
  27.       while ( !found && i < address.length)
  28.       {
  29.  
  30.         if (address[i].equals(addressnosearch) )  //could use == true
  31.         {
  32.           found = true;
  33.         }
  34.         else
  35.         {
  36.           i++;
  37.         }//end-if
  38.       }//end-while
  39.  
  40.       if (found == true)
  41.       {
  42.         Text.showMessage ( addressnosearch + " was found in the array");
  43.       }
  44.       else
  45.       {
  46.           address[activeEntries] = addressnosearch;
  47.           firstname[activeEntries] =  Text.readString("Enter First Name " );
  48.           lastname[activeEntries] =  Text.readString("Enter Last Name " );
  49.           telno[activeEntries] =  Text.readString("Enter Telephone no " );
  50.           activeEntries = activeEntries + 1;
  51.       }//end-if
  52.          }//end-while
  53.  
  54.     System.exit(0);
  55.  
  56.   }//main
  57.  
  58. }//LinearSearch
  59.  


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.NullPointerException
at test4.main(test4.java:31)
Process exited with exit code 1.
Jan 1 '08 #1
2 1342
BigDaddyLH
1,216 Expert 1GB
What is "Text"?

Also this code shows the anti-pattern "object denial":
Expand|Select|Wrap|Line Numbers
  1. String [] address = new String [maxStudents];
  2. String [] firstname = new String [maxStudents];
  3. String[] lastname = new String [maxStudents];
  4. String [] telno = new String [maxStudents];
Why not define an Addess class?
Jan 1 '08 #2
in line 30:

if (address[i].equals(addressnosearch) ) //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(addressnosearch) )
found = true;

else
i++;
.
.

hope i've helped
orev hara
Jan 1 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
3
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = =="...
9
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the...
3
by: splintercell | last post by:
well i got this code from java.sun.com and tried modiifying it in all the possible ways,but to no good.. stil its not workin..pleas help me out and try postin good workinw web cralwer if u have.....
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
16
by: timber910 | last post by:
Hello All, I've been searching for over a week and have found a good start but can't seem to make what I want work. I either get an error or a blank table. What I'm looking for. I have to make a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.