英文:
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()
butstring.length() -1
as index starts from 0. -
You don't use
casesDataArr[1]
but usecasesDataArr[0]
twiceactive = 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 = "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]
答案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(",");
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<casesDataArr.length;i++){
if(casesDataArr[i].contains("(")&&casesDataArr[i].contains(")")){
casesDataArr[i]=casesDataArr[i].substring(casesDataArr[i].indexOf("(")+1,casesDataArr[i].indexOf(")"));
}
}
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]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论