按字母顺序重新排列字符串中的单词

媒体之狼

我正在尝试重新排列任何给定字符串中的单词(20 个单词或更少)。我遇到这个问题是因为我无法打印字符串中的最后一个单词。我尝试修改循环范围,但无法解决问题。

public class ListString {
    String[] list = new String[20];
    int n = 0;

    public void read(){
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the sentence");
        String s = in.nextLine();
        String temp = "";
        for (int i=0; i<s.length(); i++)
        {
            char ch = s.charAt(i);
            if ((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))  // range from a to z
                temp = temp + ch;
            else 
            {
                if (temp.length() > 0)
                {
                    list[n] = temp;
                    n++;
                    temp = "";
                }
            }
        }

    }

    public void print(){
            System.out.print(list[0]);          
            for(int i=0;i<n;i++)
                    System.out.print(" "+list[i]);          
            System.out.println(" ");
    }

    public void sort(){
        for (int i = 0; i < n; i++) { 
        String key = list[i]; 
        int j = i - 1; 
        while (j >= 0 && (list[j].compareToIgnoreCase(key)>0)) { 
            list[j + 1] = list[j]; 
            j = j - 1; 
        } 
        list[j + 1] = key; 
    } 

    }
    
}
夫佐洛博夫

当您点击字符串的末尾并且 temp 不为空时,就会发生这种情况。要修复它,您可以在循环后添加相同的 if 语句:

for(int i = 0; i < s.length(); i++) {
    char ch = s.charAt(i);
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
        temp = temp + ch;
    } else {
        if(temp.length() > 0) {
            list[n] = temp;
            n++;
            temp = "";
        }
    }
}

if(temp.length() > 0) {
    list[n] = temp;
    n++;
    temp = "";
}

此外,您还需要修复输出以不打印第一个单词两次:

public void print() {
    for(int i = 0; i < n; i++) {
        System.out.print(list[i] + " ");
    }
    System.out.println();
}

修复前输出:

a b c d e f g h i j k l m n o p q r s t
a a b c d e f g h i j k l m n o p q r s 

修复后输出:

a b c d e f g h i j k l m n o p q r s t
a b c d e f g h i j k l m n o p q r s t 

更新:

您也可以使用在一行中解决您的问题

public void read() {
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the sentence");
    String s = in.nextLine();

    list = Arrays.stream(s.split(" ")).limit(20).sorted().toArray(String[]::new);
}

它按空格字符拆分输入字符串,取前 20 个单词,对它们进行排序并创建它们的数组。

输出:

t s r q p o n m l k j i h g f e d c b a z z z z z
a b c d e f g h i j k l m n o p q r s t 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Lua - 按字母顺序重新排列字符串

按字母顺序重新排列 URL 查询字符串

按数字顺序重新排列字符串

根据条件重新排列字符串中的单词

字符串按字母顺序排列-不按字母顺序排列第一个单词

随机重新排列单词中的字母

排序 ArrayList 以按特定字符串重新排列

按字母顺序排列的字符串中的字符,然后将按字母顺序排列的字符放入新的字符串中?

Python按字符串重新排列dat文件中的行

spark 是否按字母顺序在内部重新排列数据框中字段的顺序?

按字母顺序排列字符串

在python中重新排列字符串结构

重新排列Java中的字符串

重新排列字符串中的数字

seqecreate按字母顺序重新排列事件

重新排列输入的字符串

重新排列字符串是回文

Python函数,用于在字符的字母顺序列表中按字母顺序排列字符串

如何在jQuery选择的下拉列表中按字母顺序重新排列<options>

如何在Android Studio中按字母顺序重新排列代码?

如何使用C以字母顺序排列字符串和排列单词

c + +时,从数组随机单词不带重新排列的字符串

在Go中按字母顺序查找均等分隔的字符串/单词

在python中将随机字符串重新排列为辅音元音辅音元音顺序

重新排列列的顺序,以便特定列获得相同的字符串

根据包含日期的文本按日期重新排列一列字符串

在字符串中按字母顺序对字符进行排序

子目录中按字母顺序排列的最后一个文件中的grep字符串

在Intellij IDEA(Android studio)中按字母顺序和优先级对代码进行重新排列