Solved

Read from file, store to array, sort array

Posted on 2004-09-07
45
241 Views
Last Modified: 2010-03-31
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
Comment
Question by:luna621
  • 27
  • 13
  • 5
45 Comments
 

Author Comment

by:luna621
ID: 12003432
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
 

Author Comment

by:luna621
ID: 12003456
               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
 
LVL 92

Expert Comment

by:objects
ID: 12003459
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:luna621
ID: 12003474
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
 

Author Comment

by:luna621
ID: 12003476
Oops!  Let me check the previous Q...
0
 

Author Comment

by:luna621
ID: 12003485
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
 
LVL 92

Expert Comment

by:objects
ID: 12003507
yes

and change the output for loop to print the contents of the array
0
 

Author Comment

by:luna621
ID: 12003537
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
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 12003562
              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
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 200 total points
ID: 12003564
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12003569
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
 

Author Comment

by:luna621
ID: 12003572
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
 

Author Comment

by:luna621
ID: 12003577
Thank you objects and Giant2.  My brain is slowly coming back to java :)
0
 
LVL 92

Expert Comment

by:objects
ID: 12003595
> The summer has been too long.

Mmmmm, summer. I'm looking forward to that :)
0
 

Author Comment

by:luna621
ID: 12003597
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
 

Author Comment

by:luna621
ID: 12003602
I think what happened was it was storing the individual digits, and not the whole integer.
0
 
LVL 92

Expert Comment

by:objects
ID: 12003611
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12003619
try to use the code I referring. It sort using alphabetical order for String and numeric order for Integer.
0
 

Author Comment

by:luna621
ID: 12003622
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
 

Author Comment

by:luna621
ID: 12003630
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
 

Author Comment

by:luna621
ID: 12003681
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
 

Author Comment

by:luna621
ID: 12003711
Haha!  Figured it out... my ending } for the for loop was out of place :)
0
 

Author Comment

by:luna621
ID: 12004065
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12004078
Try to catch the exception raised up.
0
 

Author Comment

by:luna621
ID: 12004131
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
 

Author Comment

by:luna621
ID: 12004151
Oh, and my catch is not catching the FileNotFoundException either...
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12004171
But the exception raised up is a FileNotFoundException??

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

0
 

Author Comment

by:luna621
ID: 12004210
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12004237
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12004242
Could you post the full dump of the exception raised up?
0
 

Author Comment

by:luna621
ID: 12004263
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
 

Author Comment

by:luna621
ID: 12004267
And this is what happens when input.txt is blank:


Press any key to continue . . .

No error message.
0
 

Author Comment

by:luna621
ID: 12004301
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12004302
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12004312
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
 

Author Comment

by:luna621
ID: 12004323
Nope, still nothing on System.out.println()...
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12004338
Ok.
Now try to move the line I poste before between these lines:
            } // END while
//<---------------------put it here
        } // END try
0
 

Author Comment

by:luna621
ID: 12004352
Ah ha!  It worked.  Now for the FileNotFoundException... it's still giving that dump.
0
 

Author Comment

by:luna621
ID: 12004368
Ah, I fixed it.  Had to move my try { up a few lines :)
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12004370
move the line:
BufferedReader readFile = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));

in the
try{
0
 

Author Comment

by:luna621
ID: 12004376
Thank you!!!!!!   =^^=
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12004378
Ok.
Now is all working?
0
 

Author Comment

by:luna621
ID: 12004389
Yes thank you very much!!!  Do I need to give extra points for help after a question is closed?  I don't mind :)
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12004395
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
 

Author Comment

by:luna621
ID: 12004398
Okay, thank you for being so nice  :)

-goodnight!!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in @AspectJ Based AOP with Spring 2 13
servlet example 17 32
ejb on wildfly 5 20
Java: The Public Class Main 4 16
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question