仅使用某些JAVA API在String中查找最短,第二短,第二长和最长的单词

时间:2017-11-07 21:19:57

标签: java

此代码计算shortest中使用的second shortestsecond longestlongestString个字词的长度。该程序正在使用String API中的trimsubstringindexOf。可以在不使用这些API的情况下完成。 我一直在尝试仅使用.length() equals.charAt()让它工作,但我无法弄清楚如何完成它。 任何帮助将不胜感激。

    public class wordlength {  
    public static void main(String[] args) {
        String str = "Hello how are you doing";
        String trim=str.trim()+" ";
        String longest= trim.substring(0,trim.indexOf(' '));
        String seclong = trim.substring(0,trim.indexOf(' '));
        String shortest = trim.substring(0,trim.indexOf(' '));
        String secshort = trim.substring(0,trim.indexOf(' '));
        int length=trim.length();
        String temp="";

        for(int i=trim.indexOf(' ')+1;i<length;i++){
        char ch=trim.charAt(i);

            if(ch!=' ')
            temp=temp+ch;
                else{
                    if(temp.length()>longest.length()){
                        seclong=longest;
                        longest=temp;
                    }
                    else if(temp.length()>seclong.length()){
                        seclong=temp;
                    }
                    temp=" ";
                }
            }

        for(int i=trim.indexOf(' ')+1;i<length;i++){
        char ch=trim.charAt(i);

            if(ch!=' ')
            temp=temp+ch;
                else{
                    if(temp.length()<shortest.length()){
                        secshort=shortest;
                        shortest=temp;
                    }
                    else if(temp.length()<secshort.length()){
                        secshort=temp;
                    }
                    temp=" ";
                }
            }
            String space = " ";
            int shortestint = (shortest.replaceAll(space, "").length());
            int secshortint = (secshort.replaceAll(space, "").length());
            int longestint = (longest.replaceAll(space, "").length());
            int seclongint = (seclong.replaceAll(space, "").length());

            System.out.println("- The length of the shortest part is "+ shortestint + ".");
            System.out.println("- The length of the second shortest part is "+ secshortint + ".");
            System.out.println("- The length of the second longest part is "+seclongint + ".");
            System.out.println("- The length of the longest part is "+longestint + ".");

        }
    }
> The output =
> - The length of the shortest part is 3.
> - The length of the second shortest part is 3.
> - The length of the second longest part is 5.
> - The length of the longest part is 5.

1 个答案:

答案 0 :(得分:0)

这是做同样事情的另一种方式,没有substringtrimindexOf。它确实需要数组。另外,我正在使用String.split,但您可以将其替换为字符串字符的循环。

这个想法是这样的 - 我们将字符串拆分为单词,并将每个单词的长度存储在一个数组中。我们稍后会对这些长度进行排序:

String str = "Hello how are you doing";
String[] words = str.split(" ");
int [] wordLengths = new int[words.length];

for (int i=0;i<words.length;i++) {
    wordLengths[i] = words[i].length();
}

Arrays.sort(wordLengths);

System.out.println("- The length of the shortest part is " + wordLengths[0] + ".");
System.out.println("- The length of the second shortest part is " +  wordLengths[1] + ".");
System.out.println("- The length of the second longest part is " +  wordLengths[wordLengths.length-2] + ".");
System.out.println("- The length of the longest part is " +  wordLengths[wordLengths.length-1] + ".");

输出:

- The length of the shortest part is 3.
- The length of the second shortest part is 3.
- The length of the second longest part is 5.
- The length of the longest part is 5.