【问题标题】:Inputting a sorted file, unsorting it, then outputting the unsorted file (Java)输入已排序的文件,将其取消排序,然后输出未排序的文件(Java)
【发布时间】:2014-11-25 06:34:34
【问题描述】:

这个项目应该有 3 个独立的主类。 它输入一个按字母顺序排序的国家列表文件,它输出一个未排序的文件,其中行随机重新排列。

我的第一个主课是这样的:

package assignment3;
import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;

public class Assignment3 {`

public static void main(String[] args) throws Exception{
    Scanner stdIn = new Scanner(new File("C:/Users/Vicki/Desktop/CountrySortedFormat.txt"));
    PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt");

    String[] line = new String[238];
    while (stdIn.hasNextLine()){    
        for (int k = 0; k <= line.length-1; k++){
            line[k]=stdIn.nextLine();
            out.println(line[k]);
            out.close();
        }
    }  
}
}

我的代码没有任何明显的问题,但我尝试打印出数组并得到一个“null”数组。我做错了吗?

编辑:将 PrintWriter 文件名更改为 CountryUnsortedFormat

【问题讨论】:

  • 你不能用正确的答案来改变你的问题,这会导致以后来看这个问题的人产生误解。
  • 您也有我提到的多个问题并在我的回答中更正它们。

标签: java arrays file printwriter sorted


【解决方案1】:

那行:

PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountrySortedFormat.txt");

重新创建文件。在此行之后,您的输入文件将为空。

【讨论】:

  • 谢谢,我没有意识到我犯了那个错误。它应该是 CountryUNsortedFormat。
  • Jens 你是对的,正如我在回答中所说的那样,但是 OP 有多个问题,正如我在回答中提到的那样。
【解决方案2】:

除了@Jens 所说的你也有这个问题:

您的代码存在多个问题。首先,你有这行:

while (stdIn.hasNextLine())

那为什么会有这条线呢?

for (int k = 0; k <= line.length-1; k++)

你已经有一个循环,你不需要第二个循环。

你还要在每个循环中关闭输出!那是什么?

out.close();

你只需要在函数结束时关闭它!

你的循环应该是这样的:

int k = 0;
while (stdIn.hasNextLine()) {
    // for (int k = 0; k <= line.length - 1; k++) {
    line[k] = stdIn.nextLine();
    out.println(line[k]);
    k++;
    // }
}
out.close();

在这样的事情之后你的输出被排序了,你没有做任何事情来使输出未排序。

【讨论】:

  • 谢谢!这很有帮助!还有 2 个类我仍在处理,下一个类对文件进行排序。
【解决方案3】:
while (stdIn.hasNextLine()) {
            // for (int k = 0; k <= line.length - 1; k++) {
            String str = stdIn.nextLine();
            out.println(str);
            // out.close();  --> here you are closing the out put writer so after first line it will be closed.
            // }

        }

在循环外关闭扫描仪和打印机。否则在第一行之后,作者将被关闭,只打印第一行。

【讨论】:

    【解决方案4】:

    正如@Ali 所指出的,您的代码中存在多个缺陷。也没有对输入进行排序的逻辑。

    使用 ArrayList 而不是普通数组,您的取消排序将变得更容易。 这是一个示例代码,可以满足您的目的。

    import java.io.PrintWriter;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.Scanner;
    
    public class Assignment3 {
    
        public static void main(String[] args) throws Exception{
            Scanner stdIn = new Scanner(new File("sortedInput.txt"));
            PrintWriter out = new PrintWriter("unsortedOutput.txt");
    
            ArrayList<String> line = new ArrayList<String>();
            System.out.println(stdIn.hasNextLine());
            while (stdIn.hasNextLine()){
                    line.add(stdIn.nextLine());
            }
    
            //here is your unsorting
            Collections.shuffle(line);
    
            for(int i=0; i < line.size() ; i++)
                out.println(line.get(i));
    
            //Close printwriter once you are done writing everything  
            out.close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 2017-08-01
      • 1970-01-01
      • 2015-07-25
      相关资源
      最近更新 更多