簡體   English   中英

Java不接受文件作為輸入

[英]Java not accepting File as Input

我正在嘗試讀取將被排序的整數文件,然后輸出到另一個文件中。 它可以執行所有操作,除了將輸入文件放在終端中。 我不知道為什么,我包括了整個代碼,而注釋部分是似乎無效的部分(因為當我將數組硬編碼到代碼中時,它可以正常工作)。 提前致謝。

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");
    }
}

根本原因是代碼找不到您的文件。

有兩種解決方案來解決您的問題:

  1. 實例化FileReader時將絕對路徑傳遞給文件
  2. 確保文件在類路徑中,並使用getClass()。getResourceAsStream並將其傳遞給實例化Scanner(您不需要FileReader類)

我很確定*不能在正則表達式的[]內使用(“不起作用”是指它被視為實際字符,而不是1或更多)(假設您是指1或更多的白色) -空格字符)。 您可以將*放在外面:(匹配一個或多個空格字符或逗號)

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

或執行以下操作:(匹配逗號或一個或多個空格字符)

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

我不完全確定以上兩種方法都能正常工作,只是使用起來可能會更簡單:(一個或多個非數字字符)

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

注意我將*更改為+ ,否則掃描程序將匹配長度為0的字符串,並且可能逐位而不是整數地返回。

另外,您的while循環有些復雜。 這會更簡單:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM