Problem: I need to read in each line of an input file into an aray of strings.The program should quick sort the array and output the sorted list to a file:

I came out with the below code, buts its giving me two error and i need to update it in such a way that it writes the content in a new file. ALso thers is some problem at the last in implementation of scanner class.

The code follows:

import java.io.*;
import java.util.ArrayList;
import com.sun.java_cup.internal.runtime.Scanner;
import java.util.NoSuchElementException;


public class QuickSort
{

    public static void main(String[] args)
    {

        String[] items = readFile("Alaa.txt");
        printArray(items);
        String[] sortedItems = modifiedQuicksort(items);

        printArray(sortedItems);

    }

    public static void printArray(String[] ar)
    {
        for(int i = 0; i < ar.length; i++)
        {
            System.out.print(ar[i] + " ");
        }
        System.out.print("\n");
    }
    /**
     * Implementation of a QuickSort algorithm that creates two pivot
     * points. It then partitions the remaining data into three sets based
     * on their value compared to the pivots. A String[] is returned.
     */
     public static String[] modifiedQuicksort(String[] items)
     {

        if(items.length <= 1)
        {
            return items;
        }

        //Two arbitrarily assigned pivot points, 
        //in this case the first two elements
        String pivot1;
        String pivot2;

        //pivot1 will always have the smaller value
        //of the two
        if(items[0].compareTo(items[1]) < 0)
        {
            pivot1 = items[0];
            pivot2 = items[1];
        }
        else
        {
            pivot1 = items[1];
            pivot2 = items[0];
        }

        ArrayList<String> sortSetLess = new ArrayList<String>();
        ArrayList<String> sortSetMiddle = new ArrayList<String>();
        ArrayList<String> sortSetGreater = new ArrayList<String>();

        for(int i = 2; i < items.length; i++)
        {
            if(items[i].compareTo(pivot1) < 0)
            {
                sortSetLess.add(items[i]);
            }
            else if(items[i].compareTo(pivot1) >= 0 && items[i].compareTo(pivot2) <= 0)
            {
                sortSetMiddle.add(items[i]);
            }
            else if(items[i].compareTo(pivot2) > 0)
            {
                sortSetGreater.add(items[i]);
            }
        }

        //Debug printouts and arraylist to string conversions
        System.out.println("\n\n\n|---------------------|");
        System.out.println("  " + pivot1 + " " + pivot2);
        System.out.println("|---------------------|\n");

        String[] less = new String[sortSetLess.size()];
        less = sortSetLess.toArray(less);
        System.out.println("sortSetLess: " + sortSetLess);

        String[] middle = new String[sortSetMiddle.size()];
        middle = sortSetMiddle.toArray(middle);
        System.out.println("sortSetMiddle: " + sortSetMiddle);

        String[] greater = new String[sortSetGreater.size()];
        greater = sortSetGreater.toArray(greater);
        System.out.println("sortSetGreater: " + sortSetGreater);

        /* Old Code might have been the right direction...
        String[] lessSorted = modifiedQuicksort(less);
        String[] middleSorted = modifiedQuicksort(middle);
        String[] greaterSorted = modifiedQuicksort(greater);     
        String[] fullySorted = new String[3];
        */

        return concatenate(modifiedQuicksort(less), pivot1, modifiedQuicksort(middle), pivot2, modifiedQuicksort(greater));

     }

     public static String[] concatenate(String[] ar1, String p1, String[] ar2, String p2, String[] ar3)
     {

         String[] concat = new String[ar1.length + ar2.length + ar3.length + 2];

         System.arraycopy(ar1, 0, concat, 0, ar1.length);
         concat[ar1.length+1] = p1;
         System.arraycopy(ar2, 0, concat, ar1.length+2, ar2.length);
         concat[ar1.length+ar2.length+1] = p2;
         System.arraycopy(ar3, 0, concat, ar1.length+ar2.length+2, ar3.length);

         return concat;
     }

     /**
      * This method takes the name of a file as an argument, and returns
      * an array of Strings containing the strings in that file.
      * No error checking is done.
      * 
      * @fileName   The name of the file to be read.
      */
     private static String[] readFile( String fileName )
     {

        Scanner s = scannerForFile(fileName);

        if(s != null)
        {   

            ArrayList<String> items = new ArrayList<String>();

            while(true)
            {
                String next = null;
                try
                {
                    next = s.next();
                }
                catch(NoSuchElementException exc)
                {
                    // EOF
                    break;
                }
                items.add(next);
            }

            return (String[])(items.toArray(new String[]{}));
        }
        else
        {
            return new String[0];
        }
     }


     /**
      * Tries to create a scanner object for the fileName.
      * 
      * @return     The new scanner object, if available. Otherwise null.
      */
     private static Scanner scannerForFile(String fileName)
     {

        Scanner s = null;
        try
        {
            s = new Scanner(new File(fileName));     
        }   
        catch(Exception exc)
        {
            // something bad happened... 
            // can't make a scanner
            return null;
        }
        return s;       
     }
}

Code tags, formatting, and please point out the errors and where they are. You can quote a line number or highlight the error in red.

[code=JAVA] // paste code here

[/code]

Thers some problem in scanner class. Also m afraid that next() method doesnt work with Scanner class:


The code follows:
import java.io.*;
import java.util.ArrayList;
import com.sun.java_cup.internal.runtime.Scanner;
import java.util.NoSuchElementException;


public class QuickSort
{

public static void main(String[] args)
{

String[] items = readFile("Alaa.txt");
printArray(items);
String[] sortedItems = modifiedQuicksort(items);

printArray(sortedItems);

}

public static void printArray(String[] ar)
{
for(int i = 0; i < ar.length; i++)
{
System.out.print(ar + " ");
}
System.out.print("\n");
}
/**
* Implementation of a QuickSort algorithm that creates two pivot
* points. It then partitions the remaining data into three sets based
* on their value compared to the pivots. A String[] is returned.
*/
public static String[] modifiedQuicksort(String[] items)
{

if(items.length <= 1)
{
return items;
}

//Two arbitrarily assigned pivot points,
//in this case the first two elements
String pivot1;
String pivot2;

//pivot1 will always have the smaller value
//of the two
if(items[0].compareTo(items[1]) < 0)
{
pivot1 = items[0];
pivot2 = items[1];
}
else
{
pivot1 = items[1];
pivot2 = items[0];
}

ArrayList<String> sortSetLess = new ArrayList<String>();
ArrayList<String> sortSetMiddle = new ArrayList<String>();
ArrayList<String> sortSetGreater = new ArrayList<String>();

for(int i = 2; i < items.length; i++)
{
if(items.compareTo(pivot1) < 0)
{
sortSetLess.add(items);
}
else if(items.compareTo(pivot1) >= 0 && items.compareTo(pivot2) <= 0)
{
sortSetMiddle.add(items);
}
else if(items.compareTo(pivot2) > 0)
{
sortSetGreater.add(items);
}
}

//Debug printouts and arraylist to string conversions
System.out.println("\n\n\n|---------------------|");
System.out.println(" " + pivot1 + " " + pivot2);
System.out.println("|---------------------|\n");

String[] less = new String[sortSetLess.size()];
less = sortSetLess.toArray(less);
System.out.println("sortSetLess: " + sortSetLess);

String[] middle = new String[sortSetMiddle.size()];
middle = sortSetMiddle.toArray(middle);
System.out.println("sortSetMiddle: " + sortSetMiddle);

String[] greater = new String[sortSetGreater.size()];
greater = sortSetGreater.toArray(greater);
System.out.println("sortSetGreater: " + sortSetGreater);

/* Old Code might have been the right direction...
String[] lessSorted = modifiedQuicksort(less);
String[] middleSorted = modifiedQuicksort(middle);
String[] greaterSorted = modifiedQuicksort(greater);
String[] fullySorted = new String[3];
*/

return concatenate(modifiedQuicksort(less), pivot1, modifiedQuicksort(middle), pivot2, modifiedQuicksort(greater));

}

public static String[] concatenate(String[] ar1, String p1, String[] ar2, String p2, String[] ar3)
{

String[] concat = new String[ar1.length + ar2.length + ar3.length + 2];

System.arraycopy(ar1, 0, concat, 0, ar1.length);
concat[ar1.length+1] = p1;
System.arraycopy(ar2, 0, concat, ar1.length+2, ar2.length);
concat[ar1.length+ar2.length+1] = p2;
System.arraycopy(ar3, 0, concat, ar1.length+ar2.length+2, ar3.length);

return concat;
}

/**
* This method takes the name of a file as an argument, and returns
* an array of Strings containing the strings in that file.
* No error checking is done.
*
* @fileName The name of the file to be read.
*/
private static String[] readFile( String fileName )
{

Scanner s = scannerForFile(fileName);

if(s != null)
{

ArrayList<String> items = new ArrayList<String>();

while(true)
{
String next = null;
try
{
next = s.next();
}
catch(NoSuchElementException exc)
{
// EOF
break;
}
items.add(next);
}

return (String[])(items.toArray(new String[]{}));
}
else
{
return new String[0];
}
}


/**
* Tries to create a scanner object for the fileName.
*
* @return The new scanner object, if available. Otherwise null.
*/
private static Scanner scannerForFile(String fileName)
{

Scanner s = null;
try
{
s = new Scanner(new File(fileName));

}


catch(Exception exc)
{
// something bad happened...
// can't make a scanner
return null;
}return s;
}
}

Do you perhaps want to use java.util.Scanner rather than com.sun.java_cup.internal.runtime.Scanner? The errors seems to go away. Please use code tags:

[code=JAVA] // paste code here

[/code]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.