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 1509
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(String[] args)
{
Scanner data_input = new Scanner(System.in);
// Get user's name
System.out.print("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.
would it not be possible for you to tell me exactly what is wrong with the code in terms of text because to be honest i dont understand what you mean by comparing strings only or a string to an int, it doesnt matter to me which one it is as long as the search method gives results, thats why i asked for code for a search method in the first place because i have never seen or learnt how to do one before, i just found that one on the net somewhere
p.s. i dont know what object orientated code is
would it not be possible for you to tell me exactly what is wrong with the code in terms of text because to be honest i dont understand what you mean by comparing strings only or a string to an int, it doesnt matter to me which one it is as long as the search method gives results, thats why i asked for code for a search method in the first place because i have never seen or learnt how to do one before, i just found that one on the net somewhere
p.s. i dont know what object orientated code is
Java is strongly typed. Everything has a type. You have an array of ints (integers) from which you are searching for a String. You need to decide what you are searching for first, and from what. If you have an array of Strings, then you should pass an object of type String[] not int[] to your method.
BTW:If you want to program in Java, you must learn what object-oriented programming is all about. Otherwise your Java code will be slow, cumbersome, ugly, unmaintainable, e.t.c
im not bothered if my code is ugly or slow just want it to work so cant you just explain this in code please
im not bothered if my code is ugly or slow just want it to work so cant you just explain this in code please
Read my previous reply again. I'm not going to give you code that you are not willing to understand. We don't do that here. You can check our posting guidelines. If you read my previous reply, you will see how to write the method so that it works.
P.S You should really be concerned about how your code looks, it's speed, mantainability, e.t.c That is all part of programming.
ok then next time dont waste my f*cking time being an absolute d*ck head, if your not willing to help people then dont be a member of a help forum you f*cking retard, this forum is a f*cking joke that is full of f*cking geeky nerds like yourselve who think there are boss cos they know a few gay codes
ok then next time dont waste my ******* time being an absolute **** head, if your not willing to help people then dont be a member of a help forum you ******* retard, this forum is a ******* joke that is full of ******* geeky
nerds like yourselve who think there are boss cos they know a few gay codes
Information science, computer science and thus computer programming is a
branch of discrete mathematics which by itself is a branch of mathematics.
It's a science; sciences are not about 'knowing a few gay codes'.
Your remark clearly shows that you're not fit for the job. I can only guess for
the cause. Spoonfeeding you boilerplate code isn't going to do you any good;
it will be like reading the three book volumes by Donald Knuth to a tapeworm.
If you want help here you can get it, but please act as an adult and try to
understand matters you don't understand yet and don't ask for a magic wand.
kind regards,
Jos
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,...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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,...
| |