Results 1 to 20 of 20

Thread: Linear Search

  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Linear Search

    So, my linear search method isn't reading correctly. I'm not sure why. Pointers?

    Thanks.


    Java Code:
    import java.util.Scanner;
    
    public class PitcherData
    {
    public static void main(String[] args)
       {
    
          final String sent = "NoPitcher";
          final int arrSize = 100;
    
          int numRecords = 0;
          int iPos;
          int shutOut = 0;
          String nameF, nameL = " ";
          double era = 0;
    
          Scanner scan = new Scanner(System.in);
    
          String[] pitchNameF = new String[arrSize];
          String[] pitchNameL = new String[arrSize];
          int[] shutOutArr = new int[arrSize];
    
          double[] eraArr = new double[arrSize];
    
          nameF = scan.next();
          if(nameF.equals(sent))
          {
             System.out.println("Error: Sentinel rearched before loop.");
             System.exit(0);
    
       }
          if(nameF.length() > 20)
          {
             System.out.println("Error: Name too long"
                                + "Quitting program.");
             System.exit(0);
          }
    
          while(!nameF.equals(sent))
          {
             while(nameF.length() < 20)
             {
                nameF = nameF + " ";
             }
             nameL = scan.next();
             if(nameL.length() > 20)
             {
                System.out.println("Error: Name too long"
                                + "Quitting program.");
                System.exit(0);
             }
             while(nameL.length() < 20)
             {
                nameL = nameL + " ";
             }
             if(scan.hasNextInt())
             {
                shutOut = scan.nextInt();
             }
             else
             {
                System.out.println("shutOut is incorrect format. \nQuitting program.");
                System.exit(0);
             }
             if(scan.hasNextDouble())
             {
                era = scan.nextDouble();
             }
             else
             {
                System.out.println("era is in inccrrect format. \nQuitting program.");
                System.exit(0);
             }
    
    
    
             eraArr[numRecords] = era;
             pitchNameF[numRecords] = nameF;
             pitchNameL[numRecords] = nameL;
             shutOutArr[numRecords] = shutOut;
             numRecords++;
    
    
             nameF = scan.next();
             if(nameF.length() > 20)
             {
                System.out.println("Error: Name too long"
                                   + "Quitting program.");
                System.exit(0);
     
          }
          }
    
          printData(numRecords, pitchNameF, pitchNameL, eraArr, shutOutArr);
    
          nameF = scan.next();
          if(nameF.length() > 20)
          {
             System.out.println("Error: Name too long"
                                + "Quitting program.");
             System.exit(0);
          }
          while(!nameF.equals(sent))
          {
              nameL = scan.next();
              if(nameL.length() > 20)
              {
                 System.out.println("Error: Name too long"
                                    + "Quitting program.");
                 System.exit(0);
              }
    
             if(scan.hasNextInt())
             {
                shutOut = scan.nextInt();
             }
             else
             {
                System.out.println("ERA in invalid format. \n Quitting program.");
                System.exit(0);
             }
    
             iPos = seqSearch(numRecords, pitchNameF, pitchNameL, shutOutArr,
                    nameF, nameL, shutOut);
    
             System.out.println("Is there a pitcher with a name of " + nameF
                                   + " " + nameL + "\n  and a shutout of "
                                   + shutOut
                                   + " in the database,"
                                   + "\n  and if so, what is their ERA?");
    
             if(iPos > -1)
             {
                System.out.println("Yes. The ERA is " + eraArr[iPos] + ".");
             }
             else
             {
                System.out.println("No, there is no such pitcher"
                                   + "in the database.");
             }
    
             nameF = scan.next();
             if(nameF.length() > 20)
             {
                System.out.println("Error: Name too long"
                                   + "Quitting program.");
                System.exit(0);
             }
          }
       }  
       public static int seqSearch(int numRecords, String[] pitchNameF,
          String[] pitchNameL, int[] shutOutArr,
          String nameKeyF, String nameKeyL, int shutOut)
       {
          for(int i = 0; i < numRecords; i++)
             if(nameKeyF.equals(pitchNameF[i]) && nameKeyL.equals(pitchNameL[i]) )
             {
                return i;
             }
             return -1;
       }
     public static void printData(int numRecords, String[] pitchNameF,
          String[] pitchNameL, double[] eraArr, int[] shutOutArr)
       {
          for(int i = 0; i < numRecords; i++)
          {
             System.out.println("Record Number: " + i
                                + " First Name: "
                                + pitchNameF[i] + " Last Name: " + pitchNameL[i]
                                + "Shut Out Games: " + shutOutArr[i]
                                + " ERA: " + eraArr[i]);
          }
       }
    }
    
    
    
       }
    Heres just the linear search method.
    Java Code:
       public static int seqSearch(int numRecords, String[] pitchNameF,
          String[] pitchNameL, int[] shutOutArr,
          String nameKeyF, String nameKeyL, int shutOut)
       {
          for(int i = 0; i < numRecords; i++)
             if(nameKeyF.equals(pitchNameF[i]) && nameKeyL.equals(pitchNameL[i]) )
             {
                return i;
             }
             return -1;
    
    
       }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Truthfully, you won't get much help unless you be more specific with your question.

    If you're getting an error output, please copy the error details here.

    If you're getting unexpected output, please post an example of the output you're getting vs. the output you expected. Thanks

  3. #3
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Output received:
    Java Code:
    Record Number: 0 First Name: Walter               Last Name: Johnson             Shut Out Games: 110 ERA: 2.17
    Record Number: 1 First Name: Greg                 Last Name: Maddux              Shut Out Games: 35 ERA: 3.16
    Record Number: 2 First Name: Cy                   Last Name: Young               Shut Out Games: 76 ERA: 2.63
    Record Number: 3 First Name: Chisty               Last Name: Mathewson           Shut Out Games: 79 ERA: 2.13
    Is there a pitcher with a name of Greg Maddux
      and a shutout of 35 in the database,
      and if so, what is their ERA?
    No, there is no such pitcherin the database.
    Is there a pitcher with a name of Tom Seaver
      and a shutout of 61 in the database,
      and if so, what is their ERA?
    No, there is no such pitcherin the database.
    Output expected:
    Java Code:
    Record Number: 0 First Name: Walter               Last Name: Johnson             Shut Out Games: 110 ERA: 2.17
    Record Number: 1 First Name: Greg                 Last Name: Maddux              Shut Out Games: 35 ERA: 3.16
    Record Number: 2 First Name: Cy                   Last Name: Young               Shut Out Games: 76 ERA: 2.63
    Record Number: 3 First Name: Chisty               Last Name: Mathewson           Shut Out Games: 79 ERA: 2.13
    Is there a pitcher with a name of Greg Maddux
      and a shutout of 35 in the database,
      and if so, what is their ERA?
    Yes. The ERA is 3.16.
    Is there a pitcher with a name of Tom Seaver
      and a shutout of 61 in the database,
      and if so, what is their ERA?
    No, there is no such pitcherin the database.
    I apologize, I should've thought harder.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    So the only actual problem is this...

    Java Code:
    Yes. The ERA is 3.16.

    So you have quite a simple problem
    Java Code:
             if(iPos > -1)
             {
                System.out.println("Yes. The ERA is " + eraArr[iPos] + ".");
             }
             else
             {
                System.out.println("No, there is no such pitcher"
                                   + "in the database.");
             }
    The problem is: Why is iPos < -1 when it is supposed to be >-1?

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    To be honest, i don't trust the way in which your for loop is written - it could be producing unexpected results. Would you mind to try to use more conventional code and then see if the problem persists?

    Java Code:
          int indexFound = -1;
          for(int i = 0; i < numRecords; i++) {
             if(nameKeyF.equals(pitchNameF[i]) && nameKeyL.equals(pitchNameL[i]) )
             {
                indexFound = i;
             }
          }
          return indexFound;

    Now if the above still always returns -1 then it means your condition isn't set right.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I would also use .equalsIgnoreCase() rather than just .equals()

  7. #7
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    It's supposed to be greater than -1. -1 is the value returned if the first name and last name aren't the same as the key's.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    yeah I got that bit afterward, please try to follow the 2 advices I gave you in my new posts and tell us the outcome

  9. #9
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Java Code:
    Record Number: 0 First Name: Walter               Last Name: Johnson             Shut Out Games: 110 ERA: 2.17
    Record Number: 1 First Name: Greg                 Last Name: Maddux              Shut Out Games: 35 ERA: 3.16
    Record Number: 2 First Name: Cy                   Last Name: Young               Shut Out Games: 76 ERA: 2.63
    Record Number: 3 First Name: Chisty               Last Name: Mathewson           Shut Out Games: 79 ERA: 2.13
    Is there a pitcher with a name of Greg Maddux
      and a shutout of 35 in the database,
      and if so, what is their ERA?
    No, there is no such pitcherin the database.
    Is there a pitcher with a name of Tom Seaver
      and a shutout of 61 in the database,
      and if so, what is their ERA?
    No, there is no such pitcherin the database.
    Same result; I don't know how else to make the conditions though. Both the first name and last name have to match.

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i just realised.... nameKeyF is also an array, so didn't you mean to write something like the following... ?

    Java Code:
    if(nameKeyF[COLOR="red"][i][/COLOR].equals(pitchNameF[i]) && nameKeyL[COLOR="Red"][i][/COLOR].equals(pitchNameL[i])

  11. #11
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    The nameKey's aren't arrays. They are the read in values for this while loop:

    Java Code:
          while(!nameF.equals(sent))
          {
              nameL = scan.next();
              if(nameL.length() > 20)
              {
                 System.out.println("Error: Name too long"
                                    + "Quitting program.");
                 System.exit(0);
              }
    
             if(scan.hasNextInt())
             {
                shutOut = scan.nextInt();
             }
             else
             {
                System.out.println("ERA in invalid format. \n Quitting program.");
                System.exit(0);
             }
    
             iPos = seqSearch(numRecords, pitchNameF, pitchNameL, shutOutArr,
                    nameF, nameL, shutOut);
    
             System.out.println("Is there a pitcher with a name of " + nameF
                                   + " " + nameL + "\n  and a shutout of "
                                   + shutOut
                                   + " in the database,"
                                   + "\n  and if so, what is their ERA?");
    
             if(iPos > -1)
             {
                System.out.println("Yes. The ERA is " + eraArr[iPos] + ".");
             }
             else
             {
                System.out.println("No, there is no such pitcher"
                                   + "in the database.");
             }
    
             nameF = scan.next();
             if(nameF.length() > 20)
             {
                System.out.println("Error: Name too long"
                                   + "Quitting program.");
                System.exit(0);
             }
          }

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Oh, right. Well it's 3am and i'm tired and going sleep....!! Good luck matey.

  13. #13
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Figured it out, I forced the names to be (name) + blanks till it was 20 characters long THEN stored it into the array.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It seems like you have done a lot of work so this suggestion may not be the best, however; this would all be a lot easier if you made a pitcher class which encapsulated the pitcher data.

  15. #15
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    It would be better to handle it like an object, but unfortunately the assignment is to learn and work with parallel arrays. I would much rather handle it like you suggest! But I'm restricted to parallel arrays.

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Why, should have figured as much.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by Teclis View Post
    Figured it out, I forced the names to be (name) + blanks till it was 20 characters long THEN stored it into the array.
    This is the same problem you had in your other thread. What possible purpose does sticking spaces onto the end of the names have? Just store the dam name.

  18. #18
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    This is the same problem you had in your other thread. What possible purpose does sticking spaces onto the end of the names have? Just store the dam name.
    I lose points if it is not anally exactly like their sample output. It irritates me to no end.
    (So, using a for loop, I stored the names into a temp variable then added the blanks to print it out.)

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    If by "their example" you mean output, then that has nothing to do with storing the name and everything to do with displaying the name. You only add spaces when it is time to output. Placing extra spaces at the end of the name String is only going to cause you more and more problems.

  20. #20
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

Similar Threads

  1. Using Linear Search to find multiple indexes
    By Roberto1989 in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 04:36 PM
  2. problem with linear search
    By Metastar in forum New To Java
    Replies: 14
    Last Post: 09-14-2010, 08:01 PM
  3. Problem with linear search
    By Metastar in forum New To Java
    Replies: 2
    Last Post: 09-14-2010, 06:28 AM
  4. linear systems of equations
    By tyang in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 03:13 PM
  5. Replies: 1
    Last Post: 03-11-2010, 12:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •