import java.util.*;
import java.io.*;
public class App2
{
/**
* @param args the command line arguments
*/
public int count;
public String string[];
public Scanner input;
public String lines[];
App2()
{
string = new String[1000];
input = new Scanner(System.in);
count = 0;
lines = new String[32];
}
public static void main(String[] args) throws IOException
{
App2 ja = new App2();
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.heap();
}
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 void heap()
{
int i;
for (i = 0; i < lines.length; i++)
//System.out.println(" "+lines[i]);
for(i=lines.length; i>1; i--)
{
fnSortHeap(lines, i - 1);
}
System.out.println("\n Sorted array\n---------------\n");
for (i = 0; i < lines.length; i++)
System.out.println(" "+lines[i]);
}
public static void fnSortHeap(String array[], int arr_ubound){
int i = 0;
int o;
int lChild = 0;
int rChild = 0;
int mChild = 0;
int root;
String temp;
int lines1 = Integer.parseInt(array[rChild]);
int lines2 = Integer.parseInt(array[lChild]);
int lines3 = Integer.parseInt(array[mChild]);
int lines4 = Integer.parseInt(array[i]);
root = (arr_ubound-1)/2;
for(o = root; o >= 0; o--){
for(i=root;i>=0;i--){
lChild = (2*i)+1;
rChild = (2*i)+2;
if((lChild <= arr_ubound) && (rChild <= arr_ubound)){
if(lines1 >= lines2)
mChild = rChild;
else
mChild = lChild;
}
else{
if(rChild > arr_ubound)
mChild = lChild;
else
mChild = rChild;
}
if(lines4 < lines3){
temp = array[i];
array[i] = array[mChild];
array[mChild] = temp;
}
}
}
temp = array[0];
array[0] = array[arr_ubound];
array[arr_ubound] = temp;
return;
}
}
This is a assignment, yes.
We have to get a list of 1000 words, pull 32 random words from the string using a random number generator. We then list then unsorted, then use 4 different sorts. I am only working on ONE sort right now as, if I can get one working I can get the others working easily enough.
Now I know the issue I'm having is with the parseInts. But I don't know where....
Exception in thread "main" java.lang.NumberFormatException: For input string: "<First word in list here>"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at App2.fnSortHeap(App2.java:113)
at App2.heap(App2.java:98)
at App2.main(App2.java:41)
Thanks!

New Topic/Question
Reply



MultiQuote






|