打破一个置换的单词列表

时间:2015-09-13 00:03:58

标签: java arrays formatting permutation word-list

我有一个文件,它是一个置换的单词列表,格式如下。它的格式化方式,当我在记事本这样的程序中打开它时,它看起来根本没有间隔,所以例如,对于人眼来说,第一位看起来像这样:

    ATHROCYTESDISHLIKEIRRECOVERABLENESSESEMBRITTLEMENTSYOUNGSOVER

但是当我复制并通过它时,它的格式如下:

    ATHROCYTES
    DISHLIKE
    IRRECOVERABLENESSES
    EMBRITTLEMENTS
    YOUNGS
    OVER

我正在尝试将此文件加载到数组中,以便我可以对其进行排序。我正在努力如何正确地解决这个问题。我发现使用此代码:

    while (dis.available() != 0) {
            System.out.println(dis.readLine());
        }

打印出正确格式化的文档,就像我要复制并粘贴它一样。我正在使用此代码尝试将其加载到数组中:

    String[] store = sb.toString().split(",");

由于没有逗号,因此这些字词没有正确分隔。实现这一点,我也试过这个代码尝试在每个新行分割它:

    String[] store = sb.toString().split(scan.nextLine());

这两个都给了我相同的结果,这些单词被打印在同一行上。现在有人如何将我的结果正确地格式化为数组?

我已经包含了我的其余代码,因为问题可能源自其他地方:

public class InsertionSort {

public static String[] InsertSort(String[] args) {
    int i, j;
    String key;

    for (j = 1; j < args.length; j++) { //the condition has changed
        key = args[j];
        i = j - 1;
        while (i >= 0) {
            if (key.compareTo(args[i]) > 0) {//here too
                break;
            }
            args[i + 1] = args[i];
            i--;
        }
        args[i + 1] = key;
        return args;
    }

    return args;
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException, IOException {
    Scanner scan = new Scanner(System.in);
    System.out.println("Insertion Sort Test\n");


    int n;
    String name, line;


    System.out.println("Enter name of file to sort: ");
    name = scan.next();

    BufferedReader reader = new BufferedReader(new FileReader(new File(name)));
    //The StringBuffer will be used to create a string if your file has multiple lines
    StringBuffer sb = new StringBuffer();

    File file = new File(name);
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
        fis = new FileInputStream(file);

        // Here BufferedInputStream is added for fast reading.
        bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);

        // dis.available() returns 0 if the file does not have more lines.
        while (dis.available() != 0) {

  // this statement reads the line from the file and print it to
            // the console.
            System.out.println(dis.readLine());
        }

        // dispose all the resources after using them.
        fis.close();
        bis.close();
        dis.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    while((line = reader.readLine())!= null){

    sb.append(line);

}

    //We now split the line on the "," to get a string array of the values
    String[] store = sb.toString().split("/n");
     System.out.println(Arrays.toString(store));
    /* Call method sort */
    InsertSort(store);

    n = store.length;
    FileWriter fw = new FileWriter("sorted.txt");


for (int i = 0; i < store.length; i++) {
  fw.write(store[i] + "\n");
}
fw.close();
     }

}

2 个答案:

答案 0 :(得分:1)

你在这里有过早的回复声明:

  args[i + 1] = key;
  return args; // the cause
}

删除它,它应该被修复:

[ATHROCYTES, DISHLIKE, IRRECOVERABLENESSES, EMBRITTLEMENTS, YOUNGS, OVER]

 DISHLIKE -> ATHROCYTES = 3
 IRRECOVERABLENESSES -> DISHLIKE = 5
 EMBRITTLEMENTS -> IRRECOVERABLENESSES = -4
 EMBRITTLEMENTS -> DISHLIKE = 1
 YOUNGS -> IRRECOVERABLENESSES = 16
 OVER -> YOUNGS = -10
 OVER -> IRRECOVERABLENESSES = 6

[ATHROCYTES, DISHLIKE, EMBRITTLEMENTS, IRRECOVERABLENESSES, OVER, YOUNGS]

完整代码:

public static String[] InsertSort(String[] args) {
  int i, j;
  String key;

  System.out.println(Arrays.toString(args));

  for (j = 1; j < args.length; j++) { //the condition has changed
    key = args[j];
    i = j - 1;
    while (i >= 0) {
      System.out.printf(" %s -> %s = %d\n", key, args[i], key.compareTo(args[i]));
      if (key.compareTo(args[i]) > 0)//here too
        break;
      args[i + 1] = args[i];
      i--;
    }
    args[i + 1] = key;
  }

  return args;
}

public static void main(String[] args) throws FileNotFoundException, IOException {
  Scanner scan = new Scanner(System.in);
  System.out.println("Insertion Sort Test\n");

  System.out.println("Enter name of file to sort: ");
  String name = scan.nextLine();

  File file = new File(name);
  String sb = (new Scanner(file)).useDelimiter("\\Z").next();

  //We now split the line on the "," to get a string array of the values
  List<String> list = Arrays.asList(sb.split("\n\r?"));

  ArrayList<String> list2 = new ArrayList<>();
  list.stream().forEach((s) -> {
    list2.add(s.trim());
  });

  System.out.println(list2);
  /* Call method sort */
  String[] store = list2.toArray(new String[]{});

  InsertSort(store);

  System.out.println(Arrays.asList(store));

  int n = store.length;

  try (FileWriter fw = new FileWriter("sorted.txt")) {
    StringBuilder b = new StringBuilder();
    for (String s: store)
      b.append(s).append("\n");

    fw.write(b.toString());
  }
}

答案 1 :(得分:0)

您的文件在Windows记事本中显示为一行的原因可能是因为记事本仅将CRLF \n\r识别为换行符,而大多数UNIX程序仅将LF \n视为换行符。您的文本文件可能是由UNIX程序生成的。可以找到进一步的解释here.

现在,在您的代码上。

String[] store = sb.toString().split(scan.nextLine());

无论扫描仪的第一行是什么,这行代码都会提供split()。我不知道这可能是什么,但是拆分的目的是查找该项的实例,并在这些实例上对字符串进行分区。

你想要的是

String[] store = sb.toString.split("\n\r?");

String.split()接受Java正则表达式。正则表达式

"\n\r?"

相当于说'在换行中拆分,或CRLF`

此外,我建议使用Scanner解析您的字符串,而不是尝试将其拆分为数组。

Scanner scan = new Scanner(sb.toString());
while(scan.hasNextLine()) {
    //Do stuff with scan.nextLine()
}

编辑:请记住,转义字符使用后退斜杠,而不是正斜杠。例如,\n\r