为什么我的代码不计算我的字符串

时间:2018-06-13 12:15:33

标签: java arrays

我试图把长度的单词放在数组中。但它不算我的第一个字。 这是我的代码:

public class wordLength {
    public static void main(String[] args){
        String s="please come in";
        String collection="";
        String [] wordslength=new String[10];
        for(int k=0;k<s.length();k++){
            char c=s.charAt(k);
            if(c==' '){
                wordslength[collection.length()]=collection;
                collection="";
            }
            collection =collection+c;
        }
        for(int k=1;k<10;k++){
            System.out.println("at index "+ (k-1) +"result is "+wordslength[k]);
        }
    }
}

4 个答案:

答案 0 :(得分:2)

更改

for(int k=1;k<10;k++)
{
    System.out.println("at index "+ (k-1) +"result is "+wordslength[k]);
}

to 

for(int k=0;k<10;k++)
{
    System.out.println("at index "+ k +"result is "+wordslength[k]);
}

准确地解决你的问题......

public class wordLength {

    public static void main(String[] args) {
        String s = "please come in";
        String[] wordslength = new String[10];
        String[] allStrings = s.split(" ");
        for (int k = 0; k < allStrings.length; k++) {
            wordslength[allStrings[k].length()] = allStrings[k];

        }
        for (int k = 0; k < 10; k++) {
            System.out.println("at index " + (k) + "result is " + wordslength[k]);
        }
    }

}

答案 1 :(得分:0)

数组索引从0开始,你从1开始。请访问第0个索引。将其更改为。

 for(int k=1;k<10;k++){
            System.out.println("at index "+ (k-1) +"result is "+wordslength[k-1]);
        }

答案 2 :(得分:0)

你可以试试这个。

public class WordLength {

public static void main(String[] args) {
    String s = "please come in";
    String collection = "";
    String[] wordslength = new String[10];
    for (int k = 0; k < s.length(); k++) {
        char c = s.charAt(k);
        if (c == ' ') {
            wordslength[collection.length()] = collection;
            collection = "";

        }
        collection = collection + c;
    }
    for (int k = 0; k < 10; k++) {
        System.out.println("at index " + (k) + " result is " + wordslength[k]);
    }
}

}

另外请记住,{在你正在创建的身体的前一行是好的。类是大写字母。这两个只是java世界中更好的约定,以便将来更好地进行代码阅读。对于几乎所有用于格式化的IDE,Internet上都有许多样式规则。

答案 3 :(得分:0)

首先,您的代码必须更准确,我认为您应该使用ArrayList     import java.util.ArrayList;

public class wordLength {

    public static void main(String[] args)
    {
        String s="please come in";
        String collection="";
        ArrayList<String> wordslength=new ArrayList<String>();

        for(int k=0;k<s.length();k++)
        {
            char c=s.charAt(k);
            if(c==' ')
            {
                wordslength.add( collection );
                collection="";

            }
            collection = collection + c;
        }

        if( collection != " ")
        {
            wordslength.add( collection );
            collection="";

        }

        for( int k = 0 ; k < wordslength.size() ; k++ )
        {
            System.out.println( "at index " + (k) + " result is " + wordslength.get(k) + " with length " +wordslength.get(k).length() );
        }
    }

}

try it here

Aslo,请注意代码缩进和类命名法

相关问题