从文本文件中读取,对其进行排序,然后重新编写

时间:2018-04-10 05:00:26

标签: java arrays

所以我们的教授给我们分配了一个程序,该程序读取了他提供给我们的文本文件;它对它进行排序,并使用已排序的东西创建一个新文件。他希望我们从主要方法中调用三种方法,即读取,排序和写入

我已完成了一些工作,但我很困惑为io.sort提供了哪些参数。我如何将该文本事物转换为数组以提供参数这是我的代码:

public void read() {
    try {
        Scanner myLocal = new Scanner(new File("dictionary.txt"));
        while (myLocal.hasNextLine()) {
            System.out.println(myLocal.nextLine());
        }
    } catch (IOException e) {
        System.out.println(e);
    }
}

public void sort(String[] arr) {
    int n = arr.length;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1; j++) {
            /* ERROR: When j == 0, j - 1 == -1, which is out of bounds */
            if (arr[j - 1].compareTo(arr[j]) > 0) { 
                swap(j, arr);
            }
        }
    }
}

public void swap(int j, String[] arr) {
    String temp = arr[j - 1];
    arr[j - 1] = arr[j];
    arr[j] = temp;
}

public void write() {
    try {
        PrintStream writer = new PrintStream(new File("sorted.txt"));
        for (int i = 0; i < 100; i++) {
            writer.println(i);
        }
        writer.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

3 个答案:

答案 0 :(得分:1)

以下是我改变read()方法的方法:

public void read()
{
    String[] myArray = new String[1000];
    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        String a = myLocal.nextLine();
        while (myLocal.hasNextLine()){
            for (int i=0; i<myArray.length; i++){
                myArray[i] = a;
            }                   

        }
    }
    catch(IOException e){
        System.out.println(e);
    }
}

答案 1 :(得分:1)

class IO{
String[] myArray = new String[30000];

public void read()
{

    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        while (myLocal.hasNextLine()){

                for (int i=0; i<myArray.length; i++){
                String a = myLocal.nextLine();
                myArray[i] = a;

            }
        }

    }
    catch(IOException e){
        System.out.println(e);
    }
}

public void sort()
{   
    int n = myArray.length;
    for (int i=0; i<n-1; i++){
        for(int j=0; j<n-i-1; j++){
            if(myArray[j+1].compareTo(myArray[j])<0){
                String temp = myArray[j];
                myArray[j] = myArray[j+1];
                myArray[j+1] = temp;
                //toLower
            }
        }

    }



}

public void swap(int j, String[] arr)
{

    String temp = arr[j-1];
    arr[j-1] = arr[j];
    arr[j] = temp;
}

public void write()
{

    try{
        PrintStream writer = new PrintStream(new File("sorted.txt"));
        for (int i=0; i<myArray.length; i++){
            writer.println(myArray[i] + "\n");
            }
            writer.close();
    }
    catch(IOException e){
        System.out.println(e);
    }


}

}

答案 2 :(得分:1)

正确代码(已解决)

class IO {

String[] myArray = new String[30000];

public void read()
{

    try {
        Scanner myLocal = new Scanner( new File("dictionary.txt"));  
        while (myLocal.hasNextLine()){

                for (int i=0; i<myArray.length; i++){
                String a = myLocal.nextLine();
                myArray[i] = a;

            }
        }

    }
    catch(IOException e){
        System.out.println(e);
    }
}

public void sort()
{   
    int n = myArray.length;

    for (int i=0; i<n; i++){
        for(int j=1; j<n-i; j++){ 
        if (myArray[j-1].compareTo(myArray[j])>0){



                swap(j, myArray);



            }

        }


    } 



}
public void swap(int j, String[] myArray)
{
    String temp = myArray[j-1];
    myArray[j-1]=myArray[j];
    myArray[j]=temp;


}

public void write()
{
    try{
        PrintStream writer = new PrintStream(new File("myIgnoreNew.txt"));
        for (int i=0; i<myArray.length; i++){
            writer.println(myArray[i] + "\n");
            }
            writer.close();
    }
    catch(IOException e){
        System.out.println(e);
    }


}

}