Results 1 to 15 of 15
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Question problem with linear search

    So I'm lost on how to do my linear search with an input search file. I've sorted it and got everything else done fine, just can't grasp how this search is supposed to look and where I'm supposed to even start with it.

    I have 43 countries that I am to search for hits or no hits based on the country code and I have to say how many probes were used in finding the matches. The input search file has these 10 searchkeys:

    CH
    In
    ZZ
    LA
    LA
    PH
    MO
    OO
    OM
    ye

    I can post code if need be. Thanks in advance :)

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Metastar View Post
    I can post code if need be. Thanks in advance :)
    Um... without code, how can we guess what you're doing wrong?

  3. #3
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I haven't done any code for the search because I'm not sure how to start it. I'll post what I have though. I attempted to put the searchkeys into an array but I don't think it's right :/

    Java Code:
    public class Main
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws java.io.IOException
        {
            FileInputStream fis1= new FileInputStream("AsiaCountries.Fall2010.txt");
            BufferedReader br1= new BufferedReader(new InputStreamReader(fis1));
    
            String inline;
            int index = 0, index2 = 0;
            String[] searchKey = new String[11];
            Country[] myCountries;
            myCountries = new Country[43];
            Sort mySort = new Sort();
            Country[][] myCountries2 = new Country [4][];
            myCountries2[0] = new Country [6];
            myCountries2[1] = new Country [7];
            myCountries2[2] = new Country [10];
            myCountries2[3] = new Country [11];
    
            while ((inline = br1.readLine()) != null)
            {
                myCountries[index] = new Country(inline);
                index++;
    
            } // end while
    
            System.out.println("Country Name            Country Capital    " +
                    "        Country Region Name        Region #     "
                    + "Country Abbreviation   ");
            System.out.println("~~~~~~~~~~~~            ~~~~~~~~~~~~~~~            "
                    + "~~~~~~~~~~~~~~~~~~~        ~~~~~~~~     "
                    + "~~~~~~~~~~~~~~~~~~~~");
            //Formatted printout of myStates array
            for(int count=0; count < index; count++)
            {
                System.out.println(myCountries[count]);
            }//end for
    
            mySort.bubbleSort(myCountries);
    
            System.out.println("Sorted Array by Country Name\nCountry Name         "
                + "   Country Capital    " + "        Country Region Name    "
                +"    Region #     " + "Country Abbreviation   ");
                
            System.out.println("~~~~~~~~~~~~            ~~~~~~~~~~~~~~~            "
                    + "~~~~~~~~~~~~~~~~~~~        ~~~~~~~~     "
                    + "~~~~~~~~~~~~~~~~~~~~");
    
            for(int count=0; count < index; count++)
            {
                System.out.println(myCountries[count]);
            }//end for
    
            FileInputStream fis2= new FileInputStream("Search.Fall2010.txt");
            BufferedReader br2= new BufferedReader(new InputStreamReader(fis2));
    
            while ((inline = br2.readLine()) != null)
            {
                searchKey[index2] = new String (inline);
                index2++;
                
            } // end while
    
            System.out.println(index2);
            System.out.println(searchKey[index2]);
        }//end main()
    }//end Main
    Java Code:
    public class Country 
    {
        private String countryName, countryCapital, countryRegion, countryAbbrev;
        private int countryRegionNum;
        public static int countryObjCounter;
        
        public Country(String countryInput)
        {
            countryName = countryInput.substring(0,17).trim();
            countryCapital = countryInput.substring(21,40).trim();
            countryRegion = countryInput.substring(44,55).trim();
            countryRegionNum = Integer.parseInt(countryInput.substring(63,65).trim());
            countryAbbrev = countryInput.substring(66,68).trim();
            countryObjCounter++;
    
        }//end parseData Constructor()
    
        public String getCountryName()
        {
            return countryName;
        }//end getCountryName()
    
        public String getCountryCapital()
        {
            return countryCapital;
        }//end getCountryCapital()
    
        public String getCountryRegion()
        {
            return countryRegion;
        }//end getCountryRegion()
    
        public int getCountryRegionNum()
        {
            return countryRegionNum;
        }//end getCountryRegionNum()
    
        public String getCountryAbbrev()
        {
            return countryAbbrev;
        }//end getCountryAbbrev()
        
        public String toString()
        {
            return String.format("%-23s %-30s %-24s  %-16d %-4s\n",
            countryName, countryCapital,countryRegion, countryRegionNum,
            countryAbbrev);
    
        }//end toString()
    }
    Java Code:
    public class Sort
    { 
        public void bubbleSort(Country[] myCountries)
        {
            int out, in;
    
            for (out=myCountries.length - 1; out>1; out--)
                for (in=0; in<out; in++)
                    if( myCountries[in].getCountryName().compareTo
                        (myCountries[in+1].getCountryName())>0 )
                        swap (in, in+1, myCountries);
        }//end bubbleSort()
    
        private void swap(int one, int two, Country[] myCountries)
        {
            Country temp = myCountries[one];
            myCountries[one] = myCountries[two];
            myCountries[two] = temp;
        }//end swap()
    }//end Sort

  4. #4
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    What do you want to search, a file? How is your file structure? And what are you looking for?

    I didn't get that.

  5. #5
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I have 2 files I'm using, the first has 43 countries in it with the country name, capital, region(1,2,3, or 4), and country abbreviation. The second has 10 2 character country codes that have some matching the 43 country codes and some not. Also some are all caps, some are 1 cap and a lowercase, and some are all lowercase(have them above).

    I want to do a linear search with the second file searching for matches or no matches to the 10 searchkeys in the second file in comparison to the 43 two character country codes in the first file. I have all 43 countries parsed by the attributes said above(name, capital, etc.) in an array. Later on I am going to put them into a jagged array by country region(that's why I have the 2d array up top but not used anywhere in main yet).
    Last edited by Metastar; 09-14-2010 at 02:12 AM.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Linear search means use a simple for loop.

  7. #7
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Would it be a nested for to go through 2 arrays? Or is that way off?

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Metastar View Post
    Would it be a nested for to go through 2 arrays? Or is that way off?
    That's way off. I usually use nested for loops for a 2-dimensional array, not two one-dimensional arrays. Best thing for you to do: just give it a try with a for loop. You've got nothing to lose by making an attempt.

  9. #9
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    That's true, I'll give it another go.

  10. #10
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I now have this

    Java Code:
    public String linearSearch(Country[] myCountries, int first1, int last1, 
               String searchKey)
        {
            for(int i = 0; i < myCountries.length; i++)
            {
                boolean success;
                success = linearSearch(myCountries, int first2, int last2, String input[i]);
                System.out.println("Success: " + success + " " + myCountries[j].getCountryCode());
            }//end for
        }//end linearSearch
    But I still have errors and it's driving me nuts.

  11. #11
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by Metastar View Post
    But I still have errors and it's driving me nuts.
    Please copy-and-paste the exact error messages received (do not trim or crop them at all).

  12. #12
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I cant post the error msg here because its in a seperate class other than my main so i cant run it. it just says:

    unexpected type

    '.class' expected

    ';' expected

    ';' expected

    ';' expected

    not a statement

    unexpected type
    required: value
    found: class

  13. #13
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I've gotta take a break from it for tonight I'm just making it alot harder than it is and I've been working on this same small program for about 7 hours today. If you can think of anything please let me know and I'll check it out in the morning. Thanks in advance :)

  14. #14
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    The error is probably originating from this line:
    Java Code:
    linearSearch(myCountries, int first2, int last2, String input[i]);
    There's 6 problems:
    1) first2 isn't a variable
    2) last2 isn't a variable
    3) You're trying to declare first2 as an int variable as a parameter (existing parameters should be passed, not declared in a function call)
    4) Same issue with last2
    5) Same with input[i]
    6) input isn't a variable

    Perhaps some of this (such as 1, 2, and 6) are solved in code that you didn't paste.

    When posting code or errors, they have to include line numbers so we know what part of the code you're talking about. Otherwise it's just guesswork, which can waste our time and yours.

  15. #15
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    Taking the night away was what I needed hah. Figured it out pretty easily this morning after taking a look at your post Zack. I hadn't left any code out, everything you saw on the thread was all I had. Wasn't realizing what I had put in as the parameters and that I had put input for my searchkey array. Thanks for the help :)

    And I'll be sure to include line numbers in future threads :)

Similar Threads

  1. linear systems of equations
    By tyang in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 03:13 PM
  2. Replies: 1
    Last Post: 03-11-2010, 12:49 AM
  3. Reverse and Replace a String in Linear Time
    By colin.cruise in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 09:02 PM
  4. Hash table with linear probing
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:43 PM
  5. Help me(Linear Multiobjective programming)
    By vinaytvijayan in forum Advanced Java
    Replies: 2
    Last Post: 01-22-2008, 07:24 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
  •