Heap Sort - Number Format Exception

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 248 Views - Last Post: 07 February 2012 - 08:18 PM Rate Topic: -----

Topic Sponsor:

#16 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 12:26 AM

View Postjon.kiparsky, on 07 February 2012 - 12:24 AM, said:

Spend a little time with google. You should be able to work this out.



I wish that was true... that's how I stumbled into the parseInt way...

..so far my percentage of finding stuff that successfully works in Google is less than 50%... :/
..Thats why I came here, as most of the successful stuff I've found that works comes from you guys.

But thank you for pointing me in the right direction. Its 2 am here, I'll work on it more tomorrow.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1782
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 12:28 AM

Trust me, once you get what's going on with Comparable it'll be a piece of cake. Good luck.
Was This Post Helpful? 1
  • +
  • -

#18 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 05:23 PM

Okay.. I'm back, I got the heap sort in.. but I'm getting one compile error.

import java.util.*;
import java.io.*;

public class App 
        {
	 
	    /**
	     * @param args the command line arguments
	     */
	    public int count;
	    public String string[];
	    public Scanner input;
	    public String lines[]; 
	     
	    App()
	    {
	           
	        string = new String[1000];
	        input = new Scanner(System.in);
	        count = 0;
                lines = new String[32];
	    }

public static void main(String[] args) throws IOException 
        {
	          
	        App ja = new App();
	        String filename;
                
	         	         
	        if (args.length > 0)//Finds the command arguement in file
	        {
	            for(int i = 0; i < args.length; i++)
	            {
	                filename = args[i];
	                ja.test(filename);
	            }
	             
	        }
                
                ja.random();
                ja.sort(); <- Error right here.
                
        }
public void test(String inFiles) 
	     {
	         File infile = new File(inFiles);
	         String k;
	          
	          
	             try
	             {
	                 Scanner inside = new Scanner(infile);
	                  
	                 while(inside.hasNext())
	                 {
	                     k = inside.next();
	                     job(k);
	                 }
	             }
	             catch(FileNotFoundException e)
	             {
	                 System.out.println("Not working!");
	                       
	             }
	          
	     }

public void job(String result)
{

     
     string[count] = result;
     count++;
    
   

}

public void random()
{
    for(int i = 0; i < 31; i++)
    {
        Random r = new Random();
        int n = r.nextInt(999);
        lines[i] = string[n];
        System.out.println(lines[i]);
        
    }
}
public static <T extends Comparable<? super T>> void sort(T[] table)  
    {  
        buildHeap(table);  
        shrinkHeap(table);  
    }  
  
    private static <T extends Comparable<? super T>> void buildHeap(T[] table)  
    {  
        for (int child = 1; child < table.length; child++)  
        {  
            int parent = (child - 1) / 2;  
            while (parent >= 0 && table[parent].compareTo(table[child]) < 0)  
            {  
                swap(table, parent, child);  
                child = parent;  
                parent = (child - 1) / 2;  
            }  
        }  
    }  
  
    private static <T extends Comparable<? super T>> void shrinkHeap(T[] table)  
    {  
        for (int n = table.length-1; n >= 0; n--)  
        {  
            swap(table, 0, n);  
            int parent = 0;  
            while (true)  
            {  
                int leftChild = 2 * parent + 1;  
                if (leftChild >= n)  
                    break; // no more children  
                int rightChild = leftChild + 1;  
                int maxChild = leftChild;  
                if (rightChild < n && table[leftChild].compareTo(table[rightChild]) < 0)  
                    maxChild = rightChild;  
                if (table[parent].compareTo(table[maxChild]) < 0)  
                {  
                    swap(table, parent, maxChild);  
                    parent = maxChild;  
                }  
                else  
                    break; // exit loop  
            }  
        }  
    }  
  
    private static void swap(Object[] table, int i, int j)  
    {  
        Object temp = table[i];  
        table[i] = table[j];  
        table[j] = temp;  
    }  
}  




method sort in class app.App cannot be applied to given types;
required: T[]
found: no arguments
reason: cannot instantiate from arguments because actual and formal argument lists differ in length

I've been all over Google now.. and.. I'm a bit lost, I know this is a generic heapsort.. so super is involved, I just dont know where to put it.
Was This Post Helpful? 0
  • +
  • -

#19 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 06:46 PM

import java.util.*;
import java.io.*;

public class App 
        {
	 
	    /**
	     * @param args the command line arguments
	     */
	    public int count;
	    public String string[];
	    public Scanner input;
	    public String lines[]; 
	     
	    App()
	    {
	           
	        string = new String[1000];
	        input = new Scanner(System.in);
	        count = 0;
                lines = new String[32];
	    }

public static void main(String[] args) throws IOException 
        {
	          
	        App ja = new App();
	        String filename;
                
	         	         
	        if (args.length > 0)//Finds the command arguement in file
	        {
	            for(int i = 0; i < args.length; i++)
	            {
	                filename = args[i];
	                ja.test(filename);
	            }
	             
	        }
                
                ja.random();
                ja.sort();
                
        }
public void test(String inFiles) 
	     {
	         File infile = new File(inFiles);
	         String k;
	          
	          
	             try
	             {
	                 Scanner inside = new Scanner(infile);
	                  
	                 while(inside.hasNext())
	                 {
	                     k = inside.next();
	                     job(k);
	                 }
	             }
	             catch(FileNotFoundException e)
	             {
	                 System.out.println("Not working!");
	                       
	             }
	          
	     }

public void job(String result)
{

     
     string[count] = result;
     count++;
    
   

}

public void random()
{
    for(int i = 0; i < 31; i++)
    {
        Random r = new Random();
        int n = r.nextInt(999);
        lines[i] = string[n];
        System.out.println(lines[i]);
        
    }
}
public static <T extends Comparable<? super T>> void sort(T[] table)  
    {  
        buildHeap(table);  
        shrinkHeap(table);  
    }  
  
    private static <T extends Comparable<? super T>> void buildHeap(T[] table)  
    {  
        for (int child = 1; child < table.length; child++)  
        {  
            int parent = (child - 1) / 2;  
            while (parent >= 0 && table[parent].compareTo(table[child]) < 0)  
            {  
                swap(table, parent, child);  
                child = parent;  
                parent = (child - 1) / 2;  
            }  
        }  
    }  
  
    private static <T extends Comparable<? super T>> void shrinkHeap(T[] table)  
    {  
        for (int n = table.length-1; n >= 0; n--)  
        {  
            swap(table, 0, n);  
            int parent = 0;  
            while (true)  
            {  
                int leftChild = 2 * parent + 1;  
                if (leftChild >= n)  
                    break; // no more children  
                int rightChild = leftChild + 1;  
                int maxChild = leftChild;  
                if (rightChild < n && table[leftChild].compareTo(table[rightChild]) < 0)  
                    maxChild = rightChild;  
                if (table[parent].compareTo(table[maxChild]) < 0)  
                {  
                    swap(table, parent, maxChild);  
                    parent = maxChild;  
                }  
                else  
                    break; // exit loop  
            }  
        }  
    }  
  
    private static void swap(Object[] table, int i, int j)  
    {  
        Object temp = table[i];  
        table[i] = table[j];  
        table[j] = temp;  
    }  
}  



I want to say thank you all for tolerating noobs like me. You guys helped me up to this point, pointing me toward a CompareTo... but now.. I have this problem:


method sort in class app.App cannot be applied to given types;
required: T[]
found: no arguments
reason: cannot instantiate from arguments because actual and formal argument lists differ in length

I've been all over Google now.. and.. I'm a bit lost, I know this is a generic heapsort.. so super is involved, I just dont know where to put it.

The compile error is in this code: ja.sort();

I've been working on this for over a week.. I'm desperate :(
Was This Post Helpful? 0
  • +
  • -

#20 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 08:18 PM

Thanks, but you can use your previous threads instead of opening new one.
*thread merged*
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2