从字符串中解析整数android java
您好,我得到了以下代码,我用它从字符串中获取一些整数。如果索引处的字符是“-”,我可以通过将字符串与下一个数字组合来成功分离负整数和正整数,并且我可以将数字放入整数数组中...
//degree is the String i parse
String together="";
int[] info=new int[degree.length()];
int counter=0;
for (int i1 = 0; i1 < degree.length(); i1++) {
if (Character.isSpace(degree.charAt(i1))==false){
if (Character.toString(degree.charAt(i1)).equalsIgnoreCase("-")){
together="-";
i1++;
}
together = together + Character.toString(degree.charAt(i1));
info[counter]=Integer.parseInt(together);
}
else if (Character.isSpace(degree.charAt(i1))==true){
together ="";
counter++;
}
但是我遇到了这个奇怪的问题...该字符串看起来与“4 -4 90 70 40 20 0 -12”完全相同,代码解析并将整数仅放入数组中的“0”数字我的意思是我将所有数字负数和正数放入数组中,除了最后的“-12”号......有什么想法吗?
Hi i got the following code which i use to get some integers from a string. I am separating successfully the negative and positive integers by combining the string with the next number if the char at the index is "-" and i can put the numbers in an integer array...
//degree is the String i parse
String together="";
int[] info=new int[degree.length()];
int counter=0;
for (int i1 = 0; i1 < degree.length(); i1++) {
if (Character.isSpace(degree.charAt(i1))==false){
if (Character.toString(degree.charAt(i1)).equalsIgnoreCase("-")){
together="-";
i1++;
}
together = together + Character.toString(degree.charAt(i1));
info[counter]=Integer.parseInt(together);
}
else if (Character.isSpace(degree.charAt(i1))==true){
together ="";
counter++;
}
But i go this strange problem....the string looks exactly like "4 -4 90 70 40 20 0 -12" and the code parses and puts the integers into the array only to the "0" number i mean i get all the number negatives and positives into my array except the last "-12" number... any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的问题有一个更简单的解决方案:
I think there's a much simpler solution to your problem: