仅使用某些JAVA API查找字符串中最短、第二短、第二长和最长的单词

仅使用某些JAVA API查找字符串中最短、第二短、第二长和最长的单词,java,Java,此代码统计字符串中使用的最短、次短、次长和最长单词的长度。程序正在使用字符串API中的trim、substring和indexOf。这可以在不使用这些API的情况下完成。 我一直试图只使用.length()equals和.charAt()来实现它,但我不知道如何实现。 任何帮助都将不胜感激 public class wordlength { public static void main(String[] args) { String str = "Hello

此代码统计
字符串中使用的
最短
次短
次长
最长
单词的长度。程序正在使用字符串API中的
trim
substring
indexOf
。这可以在不使用这些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 + ".");

        }
    }

这里有一种做同样事情的不同方法,没有
子字符串
修剪
索引
。但它确实需要数组。另外,我使用的是
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] + ".");

请修复代码缩进以获得帮助;)
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.