简体   繁体   中英

Java not accepting File as Input

I am trying to read in a file of integers that will be sorted and then output into a different file. It can do everything except take the input file in terminal. I am not sure why, I have included the entire code and the commented portion is the part that doesn't seem to work (since when I hard code an array into the code it works fine). Thanks in advance.

class Quicksort {
    public void qSort(int[] a, int p, int r) {
        if (p < r) {
            int q = Partition(a, p, r);
            qSort(a, p, q - 1);
            qSort(a, q + 1, r);
        }
    }

    private static int Partition(int[] A, int p, int r) {
        int x = A[p];
        System.out.println(x + " ");
        int i = p;
        int j = r;
        int temp = 0;

        while (true) {
            while (A[j] > x) {
                j--;
            }

            while (A[i] < x) {
                i++;
            }

            if (i < j) {
                temp = A[j];
                A[j] = A[i];
                A[i] = temp;
            } else {
                return j;
            }
        }

    }
}

public class QuickSortTest {
    public static void main(String[] args) throws IOException {
        long time1 = System.nanoTime();
        Quicksort qsort = new Quicksort();

        //          ArrayList<Integer> dataReadIn = new ArrayList();
        //          FileReader fileToBeRead = new FileReader("test.txt");
        //          Scanner src = new Scanner(fileToBeRead);
        //          src.useDelimiter("[,\\s*]");
        //          int p = 0;
        //               while (src.hasNext()) 
        //               {
        //                   if (src.hasNext()) 
        //                   {
        //                       dataReadIn.add(src.nextInt());
        //                       System.out.println(p);
        //                       p++;
        //                     } 
        //                     else {
        //                         break;
        //                     }
        //                 }
        //           
        //      int[] array = new int[dataReadIn.size()];
        //     for(int a = 0; a < array.length; a++)
        //     {
        //     array[a] = dataReadIn.get(a);
        //     }

        int[] array = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
                79, 80, 81, 82, 83, 26, 28 };
        int length = array.length;
        System.out.println(length);
        System.out.println("Pivot Element: ");
        qsort.qSort(array, 0, length - 1);

        PrintWriter out = new PrintWriter(new FileWriter("OutputArray"));
        for (int i = 0; i < array.length; i++) {
            out.println(array[i] + " ");
        }

        out.close();
        long time2 = System.nanoTime();
        long time = (time2 - time1) / 1000;
        System.out.println("Total time: " + time + "ms");
    }
}

The root cause is your file is not found by the code.

There are 2 solutions for your problem :

  1. Pass in the absolute path to the file when instantiating FileReader
  2. Make sure the file is in the classpath and use getClass().getResourceAsStream and pass that to instantiate the Scanner (you won't need the FileReader class)

I'm pretty sure * doesn't work inside [] in a regex (by "doesn't work" I mean it's treated as the actual character, not 1-or-more) (assuming you meant 1-or-more white-space characters). You can put * outside: (matches one or more white-space characters or commas)

src.useDelimiter("[,\\s]+");

or do something like this: (match either a comma or one or more white-space characters)

src.useDelimiter("(,|\\s+)");

I'm not entirely sure either of the above will actually work, it may be simpler just to use: (one or more non-digit characters)

src.useDelimiter("[^0-9]+");

Note I changed the * to a + , otherwise the scanner will match on length 0 strings and may return digit by digit rather than a whole number.

Also, your while-loop is somewhat overcomplicated. This would be simpler:

int p = 0;
while (src.hasNext()) 
{
  dataReadIn.add(src.nextInt());
  System.out.println(p++);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM