Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

more project help please....?

  • 25-04-2007 6:22pm
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    ok this is a really tricky one that i cant figure out what to do i kind of know whats wrong but i cant figure out how to fix it, i've tried several things but to no avail...

    i've created a slectionsort object that creates an array of strings from reading in from a text file, it also has methods to display the array and sort the array.
    
    
    public class SelectionSort {
      private String[] a;
    
      private int nElems;
      String sWord;
    
      public SelectionSort(int max) {
        a = new String[max];
        nElems = 0;
      }
    
      public void insert(String value) {
        a[nElems] = value;
        nElems++;
      }
    
      public void display() {
        for (int j = 0; j < nElems; j++)
          System.out.print(a[j] + " ");
        System.out.println();
      }
    
      public void selectionSort() {
        int out, in, min;
    
    
        for (out = 0; out < nElems - 1; out++) // outer loop
        {
    
          min = out; // minimum
    
          for (in = out + 1; in < nElems; in++){
            // inner loop
    
            int x = a[in].compareTo(a[min]);
    
            if (x < 0) // if min greater,
                min = in; // a new min
    
            }
    
          swap(out, min); // swap them
        }
      }
    
    
    
      private void swap(int one, int two) {
        String temp = a[one];
        a[one] = a[two];
        a[two] = temp;
      }
    
      public String getWord()
      {
    	  for(int i = 0; i< nElems; i++)
    	  {
    		  sWord = a[i];
    
    
    	  }
    		return sWord;
      }
    

    this part works fine but i added a method called search array which is meant to search through the array and make a new array of wordcountobjects which searches the original array of strings for individual words, creates a new wordcountobject with the word and the number of times it appears
    public void SearchArray(SelectionSort[] a)
    {
    
    WordCountObject []wordcount = new WordCountObject[1000000];
    
    
    	for (int i=0; i<a.length; i++)
    
      	{
        	boolean found = false;
        	int nElems = 0;
    
    			for(int k=0;k<nElems;k++)
    			{
    				if (a[i].getWord() == wordcount[k].getWord())
        			{
          				wordcount[k].increment();
          				found = true;
          				break;
        			}
    
        			else
        			{
    					wordcount[k].addWord(a[i].getWord());
    					wordcount[k].increment();
    					nElems++;
    
    				}
    
    
      			}
    
    	}
    
    
    }
    }
    

    the problem is when i go to use the searcharray method on the original array i get this error...

    G:\Algo\project\FileTest2.java:63: SearchArray(SelectionSort[]) in SelectionSort cannot be applied to (SelectionSort)
    wordArray.SearchArray(wordArray);
    ^
    1 error

    I believe this is because the searcharray method is incompatible with the selectionsort??

    I have tried changing it to take in just a string array and added methods to get the strings out of the objects to no avail, is there something missing or is my code just a big mess that wont work at all?


Comments

  • Registered Users Posts: 4,287 ✭✭✭NotMe


    draffodx wrote:

    G:\Algo\project\FileTest2.java:63: SearchArray(SelectionSort[]) in SelectionSort cannot be applied to (SelectionSort)
    wordArray.SearchArray(wordArray);
    ^
    1 error

    You're calling searchArray() with a single SelectionSort object but it's expecting an array of SelectionSort as an argument. I guess you don't want that?


    [The class name SelectionSort is confusing. I would call it Words or something similar because basically what it is is an array of words with methods to insert words, display the words and sort the words. Having a variable of type SelectionSort called wordArray sounds kinda crazy... IMO :)
    Wouldn't something like
    Words wordArray = new Words(10000);
    
    make more sense?]

    public String getWord()
      {
    	  for(int i = 0; i< nElems; i++)
    	  {
    		  sWord = a[i];
    
    
    	  }
    		return sWord;
      }
    

    This method just returns the last word in the array. Not what you want is it?


Advertisement