I need a program that reads in each line of an input file into an array strings.The program should then quick sort the array and output the sorted list to a file.

Please help me in this ASAP.

Recommended Answers

All 6 Replies

Sounds like you better review your course notes pretty quickly, since no one here is going to do your homework for you unless you post your code and specific questions about what you're having trouble with.
(I'm guessing you don't have any code written yet).

I have an idea that I need to have a code for file reading and file writing with a code for quicksorting the string, but am unable to connect the whole. Please help me in that.

File Read Code:

import java.io.*;
public class FileRead 
{ 
    public static void main(String args[])
     {
       try
      {
                // Open the file that is the first 
               // command line parameter
      FileInputStream fstream = new FileInputStream("textfile.txt");
              // Get the object of DataInputStream
     DataInputStream in = new DataInputStream(fstream);
     BufferedReader br = new BufferedReader(new InputStreamReader(in));

             //Read File Line By Line
                  //Close the input stream
        in.close();
      }
       catch (Exception e)
         { //Catch exception if any
         System.err.println("Error: " + e.getMessage());
         }
     }
}

Sorting:

public class SortArrayString
{

public static void sortEm(String [] array, int len)
    {
    int a,b;
    String temp;
    int sortTheStrings = len - 1;
    for (a = 0; a < sortTheStrings; ++a)
    for (b = 0; b < sortTheStrings; ++b)
    if(array[b].compareTo(array[b + 1]) >0)
    {
      temp = array[b];
      array[b] = array[b + 1];
      array[b + 1] = temp;  
        }

      }
    }
}

Sounds like you better review your course notes pretty quickly, since no one here is going to do your homework for you unless you post your code and specific questions about what you're having trouble with. (I'm guessing you don't have any code written yet).

I have an idea that I need to have a code for file reading and file writing with a code for quicksorting the string , but m unabkle to connect the whole.Please help me in that.

File REad COde:
import java.io.*;
public class FileRead
{
public static void main(String args[])
{
try
{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

//Read File Line By Line
//Close the input stream
in.close();
}

catch (Exception e)
{ //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}


}


Sorting:

public class SortArrayString
{

public static void sortEm(String [] array, int len)
{

int a,b;
String temp;
int sortTheStrings = len - 1;
for (a = 0; a < sortTheStrings; ++a)
for (b = 0; b < sortTheStrings; ++b)
if(array.compareTo(array) >0)
{
temp = array;
array = array;
array = temp;
}

}
}
}

within your main method:

SortArrayString.sortEm(yourArrayName);

there's nothing to 'link' yet, since you have not yet created an array to sort.
next of, what is 'int len' supposed to be? the size of the array?

in that case, I would suggest you drop that argument, and just write in your sortEm-method:

int len = array.length;

I have an idea that I need to have a code for file reading and file writing with a code for quicksorting the string , but m unabkle to connect the whole.Please help me in that.

public class SortArrayString
{

public static void sortEm(String [] array, int len)
	{
		
		int a,b;
	    String temp;
		int sortTheStrings = len - 1;
		for (a = 0; a < sortTheStrings; ++a)
		for (b = 0; b < sortTheStrings; ++b)
		if(array[b].compareTo(array[b + 1]) >0)
		{
		  temp = array[b];
		  array[b] = array[b + 1];
		  array[b + 1] = temp;	
	    	}
	
		  }
		}
}

You want to sort the strings and then print them out to a file, so having your sortEm method return void doesn't make sense. Might want to actually return the String that is 'sorted'.

I would create a StringBuffer and as you read the file line by line keep appending the sorted string onto the end of the StringBuffer. Than when the file is done being read, write the contents of the StringBuffer back out to another file.

StringBuffer sortedStrings = new StringBuffer();
String line;

//as you read the file where br is your BufferedReader
while ( ( line = br.readLine()) != null){
          sortedStrings.append(sortEm(line));
          sortedStrings.append(System.getProperty("line.separator"));
        }

//done reading file so write the StringBuffer back to another file

Sure, you can use some built-in Java sorting functionality or take a look at quick sort tutorial, if you need to implement it by yourself.

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.