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. - import java.util.*;
-
import java.io.PrintStream;
-
import java.io.File;
-
import java.io.FileNotFoundException;
-
-
public class coursework4
-
{
-
private static final int MAX_RECORDS = 20;
-
private static String lastName[] = new String[MAX_RECORDS];
-
private static String firstName[] = new String[MAX_RECORDS];
-
private static String telNumber[] = new String[MAX_RECORDS];
-
private static String emailAddress[] = new String[MAX_RECORDS];
-
private static Scanner data_input = new Scanner(System.in);
-
-
public static int read_in_file(String file_name)
-
{
-
Scanner read_in;
-
Scanner line;
-
int record_count = 0;
-
String record;
-
-
try
-
{
-
read_in = new Scanner(new File(file_name));
-
-
// read in one line at a time
-
read_in.useDelimiter(System.getProperty("line.separator"));
-
while (read_in.hasNext())
-
{
-
// Test to see if there are too many records in the file
-
if (record_count == MAX_RECORDS)
-
{
-
System.out.printf("Only %d records allowed in file", MAX_RECORDS);
-
System.exit(0);
-
}
-
-
// read in record
-
record = new String(read_in.next());
-
-
// Split the record up into its fields and store in
-
// appropriate arrays
-
line = new Scanner(record);
-
line.useDelimiter("\\s*,,\\s*");
-
lastName[record_count] = line.next();
-
firstName[record_count] = line.next();
-
telNumber[record_count] = line.next();
-
emailAddress[record_count] = line.next();
-
-
// Increment record count
-
record_count++;
-
}
-
}
-
catch (FileNotFoundException e)
-
{
-
e.printStackTrace();
-
}
-
-
return record_count;
-
}
-
-
public static void write_out_file(int no_of_records, String filename)
-
{
-
PrintStream write_out;
-
int counter;
-
-
try
-
{
-
// Create new file
-
write_out = new PrintStream(new File(filename));
-
-
// Output all reacords
-
for(counter = 0; counter < no_of_records; counter++)
-
{
-
// Output a record
-
write_out.print(lastName[counter]);
-
write_out.print(",,");
-
write_out.print(firstName[counter]);
-
write_out.print(",,");
-
write_out.print(telNumber[counter]);
-
write_out.print(",,");
-
write_out.println(emailAddress[counter]);
-
}
-
-
// Close file
-
write_out.close();
-
}
-
catch (FileNotFoundException e)
-
{
-
e.printStackTrace();
-
}
-
}
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.
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. - import java.util.*;
-
private static String lastName[] = new String[MAX_RECORDS];
-
private static String firstName[] = new String[MAX_RECORDS];
-
private static String telNumber[] = new String[MAX_RECORDS];
-
private static String emailAddress[] = new String[MAX_RECORDS];
-
This is the Fortran way of doing things because in that language you can't do
any better than that. Think objects: -
public class Contact {
-
private String lastName;
-
private String firstName;
-
private String telNumber;
-
private String emailAddress;
-
//
-
// ctor:
-
public Contact(String lastName, String firstName,
-
String telNumber, String emailAddress) {
-
this.lastName= lastName;
-
this.firstName= firstName;
-
this.telNumber= telNumber;
-
this.emailAddress= emailAddress;
-
}
-
// getters and setters here ...
-
}
and then use a List<Contact> for your in-core database.
kind regards,
Jos
}
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
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?
Just use a loop and compare each element in the array with the entered text.
can you explain this in code please
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.
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
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.
changed it from == t to .equals(t) but it still doesnt output the record
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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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"
|
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...
|
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,...
|
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.
|
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
| |
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...
|
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 --...
|
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...
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |