Here, l have a quickSort that sorts an String array and where it starts and ends sortA(String[] s,int start, int end)
String[] wordlist = new String[5000000];
QuickSort quickSort = new QuickSort();
int number = 0, startN = 0;
public void read() throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the name of the file you want to read from");
String readFile = keyboard.nextLine();
try
{
Scanner sc2 = null;
sc2 = new Scanner(new File(readFile));
startN = number;
while (sc2.hasNextLine())
{
Scanner s2 = new Scanner(sc2.nextLine());
boolean b;
while (b = s2.hasNext())
{
String s = s2.next();
s = s.replaceAll("[^A-Za-z]","");
wordlist[number] = s;
number++;
System.out.println(s + "|" +number);
}
}
quickSort.sortA(wordlist, startN, number);
}
catch(FileNotFoundException e)
{
System.out.println("File not Found, please try again");
}
}
l am reading from a normal file and trying to sort, but it gives me :
java.lang.NullPointerException
at java.lang.String.compareTo(String.java:578)
at QuickSort.partition(QuickSort.java:21)
at QuickSort.sortA(QuickSort.java:7)
at WordMatch.read(WordMatch.java:33)
at WordMatch.main(WordMatch.java:73)
going to QuickSort.java:
public class QuickSort
{
static void sortA(String[] s, int start, int end)
{
if (end > start)
{
int pivot = partition(s, start, end);
sortA(s, start, pivot-1);
sortA(s, pivot, end);
}
}
private static int partition(String[] s, int start, int end)
{
String pivot = s[end];
int left = start;
int right = end;
String temp = "";
do
{
while ((s[left].compareTo(pivot) <= 0) && (left < end))
left++;
while ((s[right].compareTo(pivot) > 0) && (right > start))
right--;
if (left < right)
{
temp = s[left];
s[left] = s[end];
s[right] = temp;
}
} while (left < right);
temp = s[left];
s[left] = s[end];
s[end] = temp;
return left;
}
}
But it doesn't seem wrong...