Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Read from file, store to array, sort array

Hi again,

Okay, I basically got my code to read from the input file.  Check if the input file contains valid data.  Print to an output file.  Now, I need to sort the data using QuickSort.

Example: "_" represents spaces, and <tab> represents tabs

  input: 23 _ 4 _ _ 15 <tab> _ 8
output: 4 _ 8 _ 15 _ 23


---------------------------------------------------------------------------------

 import java.io.*;
 import java.util.*;

 public class ReadInputArray {

      public static void main (String[] arg) throws Exception {

        BufferedReader readFile = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
        String input = "";

        File output = new File("output.txt"); // writes to output.txt
        FileOutputStream outFromFile = new FileOutputStream(output);
        PrintStream ps = new PrintStream(outFromFile);

        ArrayList digitsList = new ArrayList(); // create new ArrayList for ints

        try {
            // Read input from input.txt
            while((input = readFile.readLine())!=null) { // reads until there are no characters left

                // separates the ints from the spaces

                StringBuffer sbDigits = new StringBuffer(); // create new string
                int len = input.length(); // length of input

                for(int i=0; i<len; i++) {
                    char c = input.charAt(i);
                    if(c == ' ' || c == '\t') { // if the character is space or tab
                        if(sbDigits.length()>0) {
                            Integer num = new Integer(sbDigits.toString());
                            digitsList.add(num + " ");
                            sbDigits = new StringBuffer();
                        } // END if
                    } // END if
                    else {
                        sbDigits.append(c);
                    } // END else

                    if(sbDigits.length()>0){
                        Integer num = new Integer(sbDigits.toString());
                        digitsList.add(num + " ");
                        sbDigits = new StringBuffer();
                    } // END if
                } // END for

                // Output the resulting array
                for(int i=0; i<digitsList.size(); i++){
                    System.out.print(digitsList.get(i));
                    ps.print(digitsList.get(i)); // writes what was read to output.txt
                } // END for
                System.out.println("\n\nProcess complete.  Please check your output.txt.\n");
            } // END while
        } // END try

        // checks for data in correct format - integers, spaces, and tabs only
        catch(NumberFormatException notInt) {
             System.out.println("Input not in correct format.  Integers, spaces, or tabs only please.");
             System.out.println("Program will now exit.\n");
        } // END catch
    } // END main()
} // END class ReadInputArray
0
luna621
Asked:
luna621
  • 27
  • 13
  • 5
2 Solutions
 
luna621Author Commented:
public class QuickSort {

// ---------------------------------------------------------
// Sorts the items in an array into ascending order.
// Precondition: theArray[first..last] is an array.
// Postcondition: theArray[first..last] is sorted.
// Calls: partition.
// ---------------------------------------------------------

    public static void quickSort(Comparable[] theArray, int first, int last) {

        int pivotIndex;

        if (first < last) {
            // create the partition: S1, Pivot, S2
            pivotIndex = partition(theArray, first, last);
            // sort regions S1 and S2
            quickSort(theArray, first, pivotIndex-1);
            quickSort(theArray, pivotIndex+1, last);
        }  // END if

    }  // END quickSort

// ---------------------------------------------------------
// Partitions an array for quicksort.
// Precondition: theArray[first..last] is an array;
// first <= last.
// Postcondition: Returns the index of the pivot element of
// theArray[first..last]. Upon completion of the method,
// this will be the index value lastS1 such that
//    S1 = theArray[first..lastS1-1] <  pivot
//         theArray[lastS1]          == pivot
//    S2 = theArray[lastS1+1..last]  >= pivot
// Calls: choosePivot.
// ---------------------------------------------------------

    private static int partition(Comparable[] theArray, int first, int last) {

        // tempItem is used to swap elements in the array
        Comparable tempItem;
        // place pivot in theArray[first]
        choosePivot(theArray, first, last);
        Comparable pivot = theArray[first];   // reference pivot

        // initially, everything but pivot is in unknown
        int lastS1 = first;          // index of last item in S1

        // move one item at a time until unknown region is empty

        for (int firstUnknown = first + 1; firstUnknown <= last; ++firstUnknown) {
        // Invariant: theArray[first+1..lastS1] < pivot
        //            theArray[lastS1+1..firstUnknown-1] >= pivot
        // move item from unknown to proper region
            if (theArray[firstUnknown].compareTo(pivot) < 0) {
                // item from unknown belongs in S1
                ++lastS1;
                tempItem = theArray[firstUnknown];
                theArray[firstUnknown] = theArray[lastS1];
                theArray[lastS1] = tempItem;
            }  // end if
            // else item from unknown belongs in S2
        }  // end for

        // place pivot in proper position and mark its location
        tempItem = theArray[first];
        theArray[first] = theArray[lastS1];
        theArray[lastS1] = tempItem;
        return lastS1;
    }  // end partition

// ---------------------------------------------------------
// Chooses a pivot for quicksort's partition algorithm and
// swaps it with the first item in an array.
// Precondition: theArray[first..last] is an array;
// first <= last.
// Postcondition: theArray[first] is the pivot.
// ---------------------------------------------------------

private static void choosePivot(Comparable[] theArray, int first, int last) {

     int pivotIndex;
     int mid = (first + last)/2;
         if (theArray[first].compareTo(theArray[mid])<=0) {
               if (theArray[mid].compareTo(theArray[last])<=0) {
                    pivotIndex = mid;
               }
               else {
                    pivotIndex = (theArray[first].compareTo(theArray[last])<=0 ? last : first);
               }
          }
          else if(theArray[mid].compareTo(theArray[last])<=0) {
               pivotIndex = mid;
          }
          else {
               pivotIndex = ((theArray[mid].compareTo(theArray[last])<=0) ? first:last);
               swap(theArray, first, pivotIndex);
          }

}  // end choosePivot


public static void swap(Comparable[] theArray, int left, int right) {

         Comparable temp = theArray[left];
         theArray[left] = theArray[right];
         theArray[right] = temp;

 } // end swap()


} // END QuickSort
0
 
luna621Author Commented:
               Comparable[] array = (Comparable[])digitsList.toArray(new Comparable[0]);
                QuickSort.quickSort(array, 0, array.length-1);

Let's see, I'm using the digitsList.  Then I typecasted it to be Comparable... the parameters fit my quicksort method, but some how it's not reading from the array.
0
 
objectsCommented:
i posted the call you need to make in the previous question

it needs to go after you have added all the numbers to your list.
ie. before the line:
   // Output the resulting array

and then output the contents of the array instead of digitsList
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
luna621Author Commented:
Maybe instead of Comparable, I need to use something else?  If that's the case, I guess I need to rewrite my QuickSort code.  So, instead of Comparable shoudl I just pass in my ArrayList?  But, then there's the problem with the .compareTo()...  


 
0
 
luna621Author Commented:
Oops!  Let me check the previous Q...
0
 
luna621Author Commented:
You're talking about this line, right?

        Comparable[] array = (Comparable[])digitsList.toArray(new Comparable[0]);
        QuickSort.quickSort(array, 0, array.length-1);


I put it right before I printed...

            while((input = readFile.readLine())!=null) { // reads until there are no characters left
                ...

                for(int i=0; i<len; i++) {
                    char c = input.charAt(i);
                    if(c == ' ' || c == '\t') { // if the character is space or tab
                        if(sbDigits.length()>0) {
                            Integer num = new Integer(sbDigits.toString());
                            digitsList.add(num + " ");
                            sbDigits = new StringBuffer();
                     } // END if
                     ...

 >>>         Comparable[] array = (Comparable[])digitsList.toArray(new Comparable[0]);         <<<
 >>>         QuickSort.quickSort(array, 0, array.length-1);         <<<

                // Output the resulting array
                for(int i=0; i<digitsList.size(); i++){
                    System.out.print(digitsList.get(i));
                    ps.print(digitsList.get(i)); // writes what was read to output.txt
                } // END for


0
 
objectsCommented:
yes

and change the output for loop to print the contents of the array
0
 
luna621Author Commented:
So... change:

                for(int i=0; i<arrayList.size(); i++){
                    System.out.print(digitsList.get(i));
                    ps.print(digitsList.get(i)); // writes what was read to output.txt
                } // END for
---------------------------------------------------------------------------------------------------------
To this??

                for(int i=0; i<array.size(); i++){
                    System.out.print(array.get(i));
                    ps.print(array.get(i)); // writes what was read to output.txt
                } // END for
0
 
objectsCommented:
              for(int i=0; i<array.length; i++){
                    System.out.print(array[i]);
                    ps.print(array[i]); // writes what was read to output.txt
                } // END for
0
 
Giant2Commented:
The QuickSort method I posted here
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21120982.html

execute only a Quicksort when in the Array of Comparable are present Integer and String (without converting them).
Do you have seen it?
0
 
Giant2Commented:
The code I posted goes well. Try it separately and then the only thing you must do to insert in your code is what I did in the main method.

Try.

Only Copy Paste Compile Run.


Bye, Giant.
0
 
luna621Author Commented:
Omg, I'm sorry!  I didn't catch that the first time.  array is with .length, and at position [].  The summer has been too long.  I apologize.  It works now.  
0
 
luna621Author Commented:
Thank you objects and Giant2.  My brain is slowly coming back to java :)
0
 
objectsCommented:
> The summer has been too long.

Mmmmm, summer. I'm looking forward to that :)
0
 
luna621Author Commented:
Oh, one more question.  My numbers are prints without spaces.  I tried putting the spaces here:

                    if(c == ' ' || c == '\t') { // if the character is space or tab
                        if(sbDigits.length()>0) {
                            Integer num = new Integer(sbDigits.toString());
                            digitsList.add(num + " "); <<------------------------------------
                            resultList.add(num + " "); <<------------------------------------
                            sbDigits = new StringBuffer();
                        } // END if

But if my input was:  16      23        99        5
The output would be: 1235699
0
 
luna621Author Commented:
I think what happened was it was storing the individual digits, and not the whole integer.
0
 
objectsCommented:
you convert your numbers to strings, so it does an alphabetic sort instead of numeric.

try:

digitsList.add(num);

and add your spacing when you output them.
0
 
Giant2Commented:
try to use the code I referring. It sort using alphabetical order for String and numeric order for Integer.
0
 
luna621Author Commented:
See, I thought I converted it to a String when I did this:

                            Integer num = new Integer(sbDigits.toString());  <----------
                            digitsList.add(num); <----------

It's still printing: 1 2 3 5 6 9 9
Instead of: 5 16 23 99
0
 
luna621Author Commented:
Comment from Giant2  feedback
Date: 09/07/2004 08:09PM HST

try to use the code I referring. It sort using alphabetical order for String and numeric order for Integer.
-------------------------------------------------------------------------------------------------------------------------------

Okay, hold on  :)


0
 
luna621Author Commented:
Okay, tried this:

                Comparable[] array = (Comparable[])digitsList.toArray(new Comparable[0]);
                QuickSort qs = new QuickSort();
                qs.quicksort(array, 0, array.length-1);

It still didn't separate the integers.  Strange thing was when I ran your code by itself, it worked.  Maybe I'm not adding it correctly into the array?
0
 
luna621Author Commented:
Haha!  Figured it out... my ending } for the for loop was out of place :)
0
 
luna621Author Commented:
Okay, last thing I promise!!

My program kinda crashes when I have a blank input.txt.  I tried fixing the problem by checking if: input = null, "", etc.  But it still doesn't work.
0
 
Giant2Commented:
Try to catch the exception raised up.
0
 
luna621Author Commented:
raised up?  This is what I tried:

        // checks for data in correct format - integers, spaces, and tabs only
        catch(FileNotFoundException e) {
            System.out.println("Can't open input.txt.  Please be sure that file exists before attempting");
            System.out.println("to run this program.");
            System.out.println("Program will now exit.\n");
            System.exit(0);
        } // END catch
        catch(NumberFormatException notInt) {
            System.out.println("Please be sure input.txt contains integers, spaces, and tabs only.");
            System.out.println("Program will now exit.\n");
            System.exit(0);
        } // END catch
        catch(IOException e) {
            System.out.println("I/O error while processing input.txt.  Please be sure the file is not blank.");
        } // END catch
        finally {
            if(readFile != null) {
                try {
                    readFile.close();
                } // END try
                catch (IOException e) {
                    System.out.println("An error occured while processing.  Please be sure you have a valid");
                    System.out.println("file named input.txt containing only integers, spaces, and tabs.");
                    System.out.println("Program will now exit.\n");
                    System.exit(0);
                } // END catch
            } // END if
        } // END finally
0
 
luna621Author Commented:
Oh, and my catch is not catching the FileNotFoundException either...
0
 
Giant2Commented:
But the exception raised up is a FileNotFoundException??

To catch all the exception could raised up you can catch Exception (in general).

0
 
luna621Author Commented:
I tried that too, and it didn't work.  Here's my whole code again:

 import java.io.*;
 import java.util.*;

 public class ReadInputArray {

      public static void main (String[] arg) throws Exception {

        BufferedReader readFile = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
        String input = "";

        File output = new File("output.txt"); // writes to output.txt
        FileOutputStream outFromFile = new FileOutputStream(output);
        PrintStream ps = new PrintStream(outFromFile);

        ArrayList digitsList = new ArrayList(); // create new ArrayList for ints

        try {
            // Read input from input.txt

            while((input = readFile.readLine())!=null) { // reads until there are no characters left

                // separates the ints from the spaces

                StringBuffer sbDigits = new StringBuffer(); // create new string
                int len = input.length(); // length of input

                // runs through entire length of string
                for(int i=0; i<len; i++) {
                    char c = input.charAt(i);
                    if(c == ' ' || c == '\t') { // if the character is space or tab
                        if(sbDigits.length()>0) {
                            Integer num = new Integer(sbDigits.toString());
                            digitsList.add(num);
                            sbDigits = new StringBuffer();
                        } // END if
                    } // END if
                    else {
                        sbDigits.append(c);
                    } // END else
                } // END for

                if(sbDigits.length()>0){
                    Integer num = new Integer(sbDigits.toString());
                    digitsList.add(num);
                    sbDigits = new StringBuffer();
                } // END if

                // Sort input array in increasing order by calling on QuickSort class
                Comparable[] array = (Comparable[])digitsList.toArray(new Comparable[0]);
                QuickSort.quickSort(array, 0, array.length-1);

                // Below is the call for the freeware function Sort.java
                //Sort.quicksort_inner(array, 0, array.length-1);

                // Output the resulting array
                for(int i=0; i<array.length; i++){
                    System.out.print(array[i] + " ");
                    ps.print(array[i] + " "); // writes what was read to output.txt
                } // END for

                System.out.println("\n\nProcess complete.  Please check your output.txt.\n");

            } // END while
        } // END try

        // checks for data in correct format - integers, spaces, and tabs only
        catch(FileNotFoundException notFound) {
            System.out.println("Can't find input.txt.  Please be sure that file exists before attempting");
            System.out.println("to run this program.");
            System.out.println("Program will now exit.\n");
            System.exit(0);
        } // END catch
        catch(NumberFormatException notInt) {
            System.out.println("Please be sure input.txt contains integers, spaces, and tabs only.");
            System.out.println("Program will now exit.\n");
            System.exit(0);
        } // END catch
        catch(IOException e) {
            System.out.println("I/O error while processing input.txt.  Please be sure the file is not blank.");
        } // END catch
        finally {
            if(readFile != null) {
                try {
                    readFile.close();
                } // END try
                catch (IOException e) {
                    System.out.println("An error occured while processing.  Please be sure you have a valid");
                    System.out.println("file named input.txt containing only integers, spaces, and tabs.");
                    System.out.println("Program will now exit.\n");
                    System.exit(0);
                } // END catch
            } // END if
        } // END finally
/*
        catch(Exception invalidEntry) {
             System.out.println("An error occured while processing.  Please be sure you have a valid");
             System.out.println("file named input.txt containing only integers, spaces, and tabs.");
             System.out.println("Program will now exit.\n");
             System.exit(0);
        } // END catch
*/
      } // END main()


 } // END class ReadInputArray
0
 
Giant2Commented:
I see 2 things:
1) The catch is commented.
2) The catch is not in the correct position (it maybe putted before finally and not after).
0
 
Giant2Commented:
Could you post the full dump of the exception raised up?
0
 
luna621Author Commented:
Oh, I commented it out because that one wasn't working.  So, I tried it individually.  Basically, when I test for a blank input.txt I get nothing.  No message, no output.txt.  When I delete the input.txt then run the program, it throws the FileNotFoundException.

I'll post the dump for the FileNotFoundException:

Exception in thread "main" java.io.FileNotFoundException: input.txt (The system
cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at ReadInputArray.main(ReadInputArray.java:23)
Press any key to continue . . .
0
 
luna621Author Commented:
And this is what happens when input.txt is blank:


Press any key to continue . . .

No error message.
0
 
luna621Author Commented:
I even tried:

            int len = input.length(); // length of input

            if(len == 0) {
                System.out.println("input.txt is blank.  Please be sure input.txt contains integers, spaces, and tabs only.");
                System.out.println("Program will now exit.\n");
                System.exit(0);
            } // END if
0
 
Giant2Commented:
So they are 2 situation.
File not exist: you must intercept it only using catch FileNotFoundException
File empty: you can intercept it looking at the len of the Array read.
0
 
Giant2Commented:
insert this line:
...
if (digitsList.size()==0) {System.out.println("There are no number to order");}
// Sort input array in increasing order by calling on QuickSort class
...
0
 
luna621Author Commented:
Nope, still nothing on System.out.println()...
0
 
Giant2Commented:
Ok.
Now try to move the line I poste before between these lines:
            } // END while
//<---------------------put it here
        } // END try
0
 
luna621Author Commented:
Ah ha!  It worked.  Now for the FileNotFoundException... it's still giving that dump.
0
 
luna621Author Commented:
Ah, I fixed it.  Had to move my try { up a few lines :)
0
 
Giant2Commented:
move the line:
BufferedReader readFile = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));

in the
try{
0
 
luna621Author Commented:
Thank you!!!!!!   =^^=
0
 
Giant2Commented:
Ok.
Now is all working?
0
 
luna621Author Commented:
Yes thank you very much!!!  Do I need to give extra points for help after a question is closed?  I don't mind :)
0
 
Giant2Commented:
Don't worry.
>Do I need to give extra points for help after a question is closed?
It's little complicated.
Don't worry.

Happy to be of some help for you.
:)
Bye, Giant.
0
 
luna621Author Commented:
Okay, thank you for being so nice  :)

-goodnight!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 27
  • 13
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now