fury283's Profile
Reputation: 0
Apprentice
- Group:
- New Members
- Active Posts:
- 12 (0.03 per day)
- Joined:
- 31-January 12
- Profile Views:
- 362
- Last Active:
Feb 07 2012 08:58 PM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Heap Sort - Number Format Exception
Posted 7 Feb 2012
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
-
In Topic: Heap Sort - Number Format Exception
Posted 7 Feb 2012
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. -
In Topic: Heap Sort - Number Format Exception
Posted 7 Feb 2012
jon.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. -
In Topic: Heap Sort - Number Format Exception
Posted 7 Feb 2012
jon.kiparsky, on 07 February 2012 - 12:17 AM, said:Previous attempt to post this failed, but:
Try using "compareTo()" method to compare the strings. Read the API description of the method, also look into the Comparable interface. This should get you where you want to be.
Do I use the compareTo in the sort? ;/ Cause we HAVE to use the sort... -
In Topic: Heap Sort - Number Format Exception
Posted 7 Feb 2012
Sorry, I'm just frustrated.. I'm not very good at coding, and thought I was getting somewhere.. guess not
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Private
Friends
fury283 hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
fury283 has no profile comments yet. Why not say hello?