473,396 Members | 1,765 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.

search method help

18
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 1509
JosAH
11,448 Expert 8TB
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
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 8TB
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
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 8TB
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
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
May 15 '07 #7
r035198x
13,262 8TB
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
changed it from == t to .equals(t) but it still doesnt output the record
May 15 '07 #9
r035198x
13,262 8TB
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
djtosh
18
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
May 15 '07 #11
r035198x
13,262 8TB
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
May 15 '07 #12
djtosh
18
im not bothered if my code is ugly or slow just want it to work so cant you just explain this in code please
May 15 '07 #13
r035198x
13,262 8TB
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.
May 15 '07 #14
djtosh
18
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
May 15 '07 #15
JosAH
11,448 Expert 8TB
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
May 15 '07 #16

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

Similar topics

1
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,...
6
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"...
3
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...
8
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...
2
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...
2
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...
0
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...
5
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...
2
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...
0
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...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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,...
0
jinu1996
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...
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.