search method help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • djtosh
    Banned
    New Member
    • Mar 2007
    • 18

    search method help

    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.

    Code:
    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.
    Last edited by JosAH; May 14 '07, 07:39 PM. Reason: fixed tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by djtosh
    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.

    Code:
    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:
    Code:
    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
    }

    Comment

    • djtosh
      Banned
      New Member
      • Mar 2007
      • 18

      #3
      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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by djtosh
        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?

        Comment

        • djtosh
          Banned
          New Member
          • Mar 2007
          • 18

          #5
          Just use a loop and compare each element in the array with the entered text.
          can you explain this in code please

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by djtosh
            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.

            Comment

            • djtosh
              Banned
              New Member
              • Mar 2007
              • 18

              #7
              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

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by djtosh
                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.

                Comment

                • djtosh
                  Banned
                  New Member
                  • Mar 2007
                  • 18

                  #9
                  changed it from == t to .equals(t) but it still doesnt output the record

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by djtosh
                    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.

                    Comment

                    • djtosh
                      Banned
                      New Member
                      • Mar 2007
                      • 18

                      #11
                      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

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by djtosh
                        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

                        Comment

                        • djtosh
                          Banned
                          New Member
                          • Mar 2007
                          • 18

                          #13
                          im not bothered if my code is ugly or slow just want it to work so cant you just explain this in code please

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by djtosh
                            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.
                            Last edited by r035198x; May 15 '07, 12:12 PM. Reason: forgot to add the PS

                            Comment

                            • djtosh
                              Banned
                              New Member
                              • Mar 2007
                              • 18

                              #15
                              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

                              Comment

                              Working...