473,729 Members | 2,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

search method help

18 New Member
hi there hoping someone can help me out with this.

im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their last name.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.PrintStream;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5.  
  6. public class coursework4
  7. {
  8.     private static final int MAX_RECORDS = 20;
  9.     private static String lastName[] = new String[MAX_RECORDS];
  10.     private static String firstName[] = new String[MAX_RECORDS];
  11.     private static String telNumber[] = new String[MAX_RECORDS];
  12.     private static String emailAddress[] = new String[MAX_RECORDS];
  13.     private static Scanner data_input = new Scanner(System.in);
  14.  
  15.     public static int read_in_file(String file_name)
  16.     {
  17.         Scanner read_in;
  18.         Scanner line;
  19.         int record_count = 0;
  20.         String record;
  21.  
  22.         try
  23.         {
  24.             read_in = new Scanner(new File(file_name));
  25.  
  26.             // read in one line at a time
  27.             read_in.useDelimiter(System.getProperty("line.separator")); 
  28.             while (read_in.hasNext())
  29.             {
  30.                 // Test to see if there are too many records in the file
  31.                 if (record_count == MAX_RECORDS)
  32.                 {
  33.                     System.out.printf("Only %d records allowed in file", MAX_RECORDS);
  34.                     System.exit(0);
  35.                 }
  36.  
  37.                 // read in record
  38.                 record = new String(read_in.next());
  39.  
  40.                 // Split the record up into its fields and store in
  41.                 // appropriate arrays
  42.                 line = new Scanner(record);
  43.                 line.useDelimiter("\\s*,,\\s*");
  44.                 lastName[record_count] = line.next();
  45.                 firstName[record_count] = line.next();
  46.                 telNumber[record_count] = line.next();
  47.                 emailAddress[record_count] = line.next();
  48.  
  49.                 // Increment record count
  50.                 record_count++;
  51.             }
  52.         }
  53.         catch (FileNotFoundException e) 
  54.         {
  55.             e.printStackTrace();
  56.         }
  57.  
  58.         return record_count;
  59.     }
  60.  
  61.     public static void write_out_file(int no_of_records, String filename)
  62.     {
  63.         PrintStream write_out;
  64.         int counter;
  65.  
  66.         try
  67.         {
  68.             // Create new file
  69.             write_out = new PrintStream(new File(filename));
  70.  
  71.             // Output all reacords
  72.             for(counter = 0; counter < no_of_records; counter++)
  73.             {
  74.                 // Output a record
  75.                 write_out.print(lastName[counter]);
  76.                 write_out.print(",,");
  77.                 write_out.print(firstName[counter]);
  78.                 write_out.print(",,");
  79.                 write_out.print(telNumber[counter]);
  80.                 write_out.print(",,");
  81.                 write_out.println(emailAddress[counter]);
  82.             }
  83.  
  84.             // Close file
  85.             write_out.close();
  86.         }
  87.         catch (FileNotFoundException e) 
  88.         {
  89.             e.printStackTrace();
  90.         }
  91.     }
this is the code i have for reading the data in. i dont know how i can go about coding a search method. hopefully someone can help me thanks.
May 14 '07 #1
15 1530
JosAH
11,448 Recognized Expert MVP
hi there hoping someone can help me out with this.

im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their last name.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.     private static String lastName[] = new String[MAX_RECORDS];
  3.     private static String firstName[] = new String[MAX_RECORDS];
  4.     private static String telNumber[] = new String[MAX_RECORDS];
  5.     private static String emailAddress[] = new String[MAX_RECORDS];
  6.  
This is the Fortran way of doing things because in that language you can't do
any better than that. Think objects:
Expand|Select|Wrap|Line Numbers
  1. public class Contact {
  2.    private String lastName;
  3.    private String firstName;
  4.    private String telNumber;
  5.    private String emailAddress;
  6.    //
  7.    // ctor:
  8.    public Contact(String lastName, String firstName, 
  9.                   String telNumber, String emailAddress) {
  10.       this.lastName= lastName;
  11.       this.firstName= firstName;
  12.       this.telNumber= telNumber;
  13.       this.emailAddress= emailAddress;
  14.    }
  15.    // getters and setters here ...
  16. }
and then use a List<Contact> for your in-core database.

kind regards,

Jos
}
May 14 '07 #2
djtosh
18 New Member
hi there thanks for replying but i cant see how that helps me, i need to know how to code a search method that will start with getting text from the user and then look in to the lastName array and see which records have the text in it.

static int find(int[] A, int N) {
// Searches the array A for the integer N.

for (int index = 0; index < A.length; index++)
{
if ( A[index] == N )
return index; // N has been found at this index!
}
something similar to these i think but applying to my code, i cnt work out what goes where
May 14 '07 #3
r035198x
13,262 MVP
hi there thanks for replying but i cant see how that helps me, i need to know how to code a search method that will start with getting text from the user and then look in to the lastName array and see which records have the text in it.



something similar to these i think but applying to my code, i cnt work out what goes where
Just use a loop and compare each element in the array with the entered text.
BTW Why are you using Java if you're not willing to think objects in your program?
May 15 '07 #4
djtosh
18 New Member
Just use a loop and compare each element in the array with the entered text.
can you explain this in code please
May 15 '07 #5
r035198x
13,262 MVP
can you explain this in code please
Why don't you write the code first and we'll see if you need more help on it.

BTW:You should really consider using objects.
May 15 '07 #6
djtosh
18 New Member
if i knew how to write the code i wouldnt be asking for it

public static void textsearch(Stri ng[] args)
{
Scanner data_input = new Scanner(System. in);
// Get user's name
System.out.prin t("Please enter search parameters: ");
String t = data_input.next ();

static int find(int[] lastName, int t)
{


for (int index = 0; index < lastName.length ; index++)
if ( lastName[index] == t )
return index;
}


}
this is what i tried before i came on here and it doesnt work
May 15 '07 #7
r035198x
13,262 MVP
if i knew how to write the code i wouldnt be asking for it



this is what i tried before i came on here and it doesnt work
Do not use == to compare two Strings. Use the .equals method.
May 15 '07 #8
djtosh
18 New Member
changed it from == t to .equals(t) but it still doesnt output the record
May 15 '07 #9
r035198x
13,262 MVP
changed it from == t to .equals(t) but it still doesnt output the record
Wait a minute. There's something going on here.
You have an int[] not a String[]? So you couldn't have out the .equals method correctly in there. Are you comparing strings only or a string to an int.

BTW:Again I have to point out the need to write object oriented code if you have decided to use Java.
May 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
8726
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast, returned the hyperlinked page title, filename, and the body txt (30 preceding and following words) in context with the search word highlighted. Excellent.! See it working at: http://www.ipt.co.za Just search for "firearm"
6
1962
by: Ward Cleaver | last post by:
Hi. I have an assignment to do some validating of a form using javascript and mostly the search() method. I'm having problems getting a positive validation for phone numbers like "123-456-7890" and "123.456.7890" but not like "123.456-7890". The regular expression I'm using now is something like this: ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3}.\d{3}.\d{4}$)/); Which to me looks like it SHOULD do what I want it to and not come back...
3
2224
by: Liz Malcolm | last post by:
Hello and TIA for guidance. I am building a reusable search procedure (thanks go to Graham Thorpe for his example that set me on my way). Everything works up until the 2nd match is found, the command doesn't go to the third match, it exits out of the loop. I've read everything I could find in NG's, but I can't figure out where I'm going wrong. Here is my code. Function cmdSearch(ctlSearchText As Control, ctlFoundText As Control,...
8
3221
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled 'search' may be clicked on by the user and the user can then search all records by postcode. I want to do this to prevent duplicate data entry.
2
26921
by: Alphonse Giambrone | last post by:
Is there a way to use multiple search patterns when calling Directory.GetFiles. For instance Directory.GetFiles("C:\MyFolder", "*.aspx") will return all files with the aspx extension. But what if I wanted all files with either an aspx OR htm extension? Any way to get that without calling the method twice? -- Alphonse Giambrone Email: a-giam at customdatasolutions dot us
2
2756
by: Scott | last post by:
I'm trying to use the HTMLHelp API calls in a VB.NET program because I want a little more functionality than is offered by the Help class in .NET. Everything works fine except for displaying the "Search" tab. Has anyone gotten this to work? This is what I have so far.... The HTMLHelp API call always returns 0... ' HTML Help function for showing search Private Const HH_DISPLAY_SEARCH As Short = &H3 Private Declare Function...
0
2080
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently structured relational database system. The CMS stores articles. The CMS also stores related items --...
5
5272
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method working. But before I get to the remove, I need to get my find method working. Basically, I am trying to get a "find" method working that will search for a giving int value, and return the node with that value. I have designed my current find with the...
2
2659
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
0
10772
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.