【问题标题】:how to remove empty line when read from file and sort the double linked list从文件中读取时如何删除空行并对双链表进行排序
【发布时间】:2012-11-04 16:34:38
【问题描述】:

1-从文本文件中读取单词时有空行

示例:

  output:
   catholicate 
   chymia

   sitosterol 
   Aries 

2- 我将单词保存在双链表中,但它没有排序,我如何在双链表中按字母顺序对其进行排序。

该方法,但我没有将保存方法放在双链表中:

public static void main(String[] args)throws IOException {

    Scanner input;
    try{
        input = new Scanner(new File(""));
        String c;
        int i=0 ;
        input.useDelimiter(" |:|,|\\.");

        while(input.hasNext()) {
            c=input.next();
            System.out.println(c);
        }
        input.close();
    }
    catch (Exception e){
        System.out.println("error");
    }

【问题讨论】:

    标签: java sorting java.util.scanner


    【解决方案1】:

    使用hasNextLine()nextLine如下:

        String line = null;
        while(input.hasNextLine()) {
            line=input.nextLine();
            //print the line only when it has some characters
            if(line.length()>0){
              System.out.println(line);
            }
        }
    

    【讨论】:

      【解决方案2】:

      只检查length of line,如果是0则忽略它

      【讨论】:

        【解决方案3】:

        你似乎在为自己把事情复杂化。你应该考虑我至少需要做什么才能完成这项工作。让程序变得简单并不容易。

        从文本文件中读取单词时有空行

        您必须将行添加到链接列表中。如果您跳过空白行,您将“删除”它们。

        我将单词保存在双链表中,但它没有排序,我如何在双链表中按字母顺序对其进行排序。

        如果您使用的是 LinkedList,您可以致电 Collections.sort() 如果您使用的是您自己的链表实现,您需要将代码提供给我们。

        【讨论】:

        • 我一次只看一行。 Scanner 是比 StringTokenizer 更好的选择
        猜你喜欢
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        • 1970-01-01
        • 2011-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多