无法将字符串转换为整数,在Java中无法获取数组元素的长度。

huangapple go评论77阅读模式
英文:

Not able to convert string to int, not able to get length of array element in java

问题

I have a String amount having value 496988,(11874),952743,(35175),33425,(654)

I'm splitting it so I can get individual element in String[] casesDataArr

String[] casesDataArr = textView.getText().toString().split(" ");

Now I just need the integers from the array and remove the (, )

I tried this

active = Integer.parseInt(casesDataArr[0]);
delActive = Integer.parseInt(casesDataArr[1].substring(1, casesDataArr[1].length() - 1));

discharged = Integer.parseInt(casesDataArr[2]);
delDischarged = Integer.parseInt(casesDataArr[3].substring(1, casesDataArr[3].length() - 1));

death = Integer.parseInt(casesDataArr[4]);
delDeath = Integer.parseInt(casesDataArr[5].substring(1, casesDataArr[5].length() - 1));

but getting an error in the last line:

java.lang.StringIndexOutOfBoundsException: length=5; index=6

I know it's due to the casesDataArr.length but when I write casesDataArr[5].length it shows redundant text. I'm missing something, do guide me. Having any doubts comment below.

英文:

I have a String amount having value 496988,(11874),952743,(35175),33425,(654)

I'm splitting it so I can get individual element in String[] casesDataArr

String[] casesDataArr = textView.getText().toString().split(" ");

Now I just need the integers from array and remove the ( , )

I tried this

active = Integer.parseInt(casesDataArr[0]);
delActive = Integer.parseInt(casesDataArr[1].substring(1, casesDataArr.length));

discharged = Integer.parseInt(casesDataArr[2]);
delDischarged = Integer.parseInt(casesDataArr[3].substring(1, casesDataArr.length));

death = Integer.parseInt(casesDataArr[4]);
delDeath = Integer.parseInt(casesDataArr[5].substring(1, casesDataArr.length));

but getting error in last line:
> java.lang.StringIndexOutOfBoundsException: length=5; index=6

I know it's due to the casesDataArr.length but when I write casesDataArr[5].length it shows redundant text. I'm missing something do guide me. Having any doubts comment below.

答案1

得分: 1

  • 你在使用数组的长度而不是每个字符串的长度。应该让你意识到这一点的是,你对每个索引都使用了相同的长度,这不奇怪吗?

  • 字符串的最后一个字符的索引不是string.length(),而是string.length() - 1,因为索引从0开始。

  • 你没有使用casesDataArr[1],但使用了两次casesDataArr[0]

active = Integer.parseInt(casesDataArr[0]);
delActive = Integer.parseInt(casesDataArr[1].substring(1, casesDataArr[1].length() - 1));

discharged = Integer.parseInt(casesDataArr[2]);
delDischarged = Integer.parseInt(casesDataArr[3].substring(1, casesDataArr[3].length() - 1));

death = Integer.parseInt(casesDataArr[4]);
delDeath = Integer.parseInt(casesDataArr[5].substring(1, casesDataArr[5].length() - 1));
英文:

3 issues:

  • You are using the legnth of the array instead of the length of each string. What should have lead you to this is that you use the same length for each index, isn't that weird?)

  • The index of the last character of a String is not string.length()but string.length() -1 as index starts from 0.

  • You don't use casesDataArr[1] but use casesDataArr[0] twice

    active = Integer.parseInt(casesDataArr[0]);
    delActive = Integer.parseInt(casesDataArr[1].substring(1, casesDataArr[1].length()-1));
    
    discharged = Integer.parseInt(casesDataArr[2]);
    delDischarged = Integer.parseInt(casesDataArr[3].substring(1, casesDataArr[3].length()-1));
    
    death = Integer.parseInt(casesDataArr[4]);
    delDeath = Integer.parseInt(casesDataArr[5].substring(1, casesDataArr[5].length()-1));
    

答案2

得分: 1

Using regex, you can remove non-numeric characters:

String input = "496988,(11874),952743,(35175),33425,(654)";

List<Integer> values = new ArrayList<>();
for (String s : input.split(",")) {
    values.add(Integer.parseInt(s.replaceAll("\\D", "")));
}

System.out.println("values: " + values);

Result:

values: [496988, 11874, 952743, 35175, 33425, 654]

英文:

Using regex, you can remove non-numeric characters:

String input = &quot;496988,(11874),952743,(35175),33425,(654)&quot;;

List&lt;Integer&gt; values = new ArrayList&lt;&gt;();
for (String s : input.split(&quot;,&quot;)) {
    values.add(Integer.parseInt(s.replaceAll(&quot;\\D&quot;, &quot;&quot;)));
}

System.out.println(&quot;values: &quot; + values);

Result:

> values: [496988, 11874, 952743, 35175, 33425, 654]

答案3

得分: 1

给定字符串值:496988,(11874),952743,(35175),33425,(654)

首先移除“,”:

String casesDataArr[] = textView.getText().toString().split(",");

现在你的数组如下:

索引 0: 496988

索引 1: (11874)

索引 2: 952743

索引 3: (35175)

索引 4: 33425

索引 5: (654)

其次,如果存在“()”,则移除:

for (int i = 0; i < casesDataArr.length; i++) {
    if (casesDataArr[i].contains("(") && casesDataArr[i].contains(")")) {
        casesDataArr[i] = casesDataArr[i].substring(casesDataArr[i].indexOf("(") + 1, casesDataArr[i].indexOf(")"));
    }
}

得到的数组为:

索引 0: 496988

索引 1: 11874

索引 2: 952743

索引 3: 35175

索引 4: 33425

索引 5: 654

要从数组的索引中提取一个int类型的数字,只需编写:

int myInteger = Integer.parseInt(casesDataArr[0]);
英文:

Given: String of value : 496988,(11874),952743,(35175),33425,(654)

First remove ",":

String casesDataArr[]=textView.getText().toString().split(&quot;,&quot;);

Your array is now as follows:

index 0: 496988

index 1: (11874)

index 2: 952743

index 3: (35175)

index 4: 33425

index 5: (654)

Second, remove "()" if present:

for(int i=0;i&lt;casesDataArr.length;i++){

if(casesDataArr[i].contains(&quot;(&quot;)&amp;&amp;casesDataArr[i].contains(&quot;)&quot;)){
casesDataArr[i]=casesDataArr[i].substring(casesDataArr[i].indexOf(&quot;(&quot;)+1,casesDataArr[i].indexOf(&quot;)&quot;));
}

}

The obtained array is:

index 0: 496988

index 1: 11874

index 2: 952743

index 3: 35175

index 4: 33425

index 5: 654

To fetch a number in the int type from an index of the array, just write this:

int myInteger=Integer.parseInt(casesDataArr[0]);

huangapple
  • 本文由 发表于 2020年7月28日 20:45:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63134445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定