简体   繁体   English

来自命令行的数组中字符串的长度

[英]Length of string in the array from command line

From the command line i give values of the array:从命令行我给出数组的值:

> java program Hello HelloWorld "Hello World"

I need to count length of each value.我需要计算每个值的长度。 I supposed to receive { 5, 10, 2 } but my code is couting each separate character in "Hello World" input instead of taking each word as a 1 string.我应该收到 { 5, 10, 2 } 但我的代码正在计算“Hello World”输入中的每个单独字符,而不是将每个单词作为 1 字符串。 How can I do so?我该怎么做?

From the command line i give values of the array:从命令行我给出数组的值:

> java program Hello HelloWorld "Hello World"

I need to count length of each value.我需要计算每个值的长度。 I supposed to receive { 5 , 10 , 2 } but my code is couting each separate character in "Hello World" input instead of taking each word as a 1 string.我应该收到 { 5 , 10 , 2 } 但我的代码在“Hello World”输入中计算每个单独的字符,而不是将每个单词作为 1 个字符串。 How can I do so?我怎么能这样做?

lets say your string is called Arr and already has the values inside- you can create a loop which goes through each spot of the array and you can check the length of the string.假设您的字符串称为 Arr 并且内部已经包含值 - 您可以创建一个遍历数组每个点的循环,并且可以检查字符串的长度。 also, create a integer array lets call it ar - int[] ar= new ar[arr.length] which will contain the length of the strings values.另外,创建一个 integer 数组,我们称它为 ar - int[] ar= new ar[arr.length] ,它将包含字符串值的长度。

for(int i=0; i<Arr.length;i++){
    Ar[i]= arr[i].length();// or loop through the string value and use variable count to count the letters.
}

good luck.祝你好运。 hope this works希望这有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM