从字符串中解析整数android java

发布于 2024-12-04 04:07:04 字数 912 浏览 3 评论 0原文

您好,我得到了以下代码,我用它从字符串中获取一些整数。如果索引处的字符是“-”,我可以通过将字符串与下一个数字组合来成功分离负整数和正整数,并且我可以将数字放入整数数组中...

   //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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-12-11 04:07:04

我认为您的问题有一个更简单的解决方案:

// First split the input String into an array,
// each element containing a String to be parse as an int
String[] intsToParse = degree.split(" ");

int[] info = new int[intsToParse.length];

// Now just parse each part in turn
for (int i = 0; i < info.length; i++)
{
    info[i] = Integer.parseInt(intsToParse[i]);
}

I think there's a much simpler solution to your problem:

// First split the input String into an array,
// each element containing a String to be parse as an int
String[] intsToParse = degree.split(" ");

int[] info = new int[intsToParse.length];

// Now just parse each part in turn
for (int i = 0; i < info.length; i++)
{
    info[i] = Integer.parseInt(intsToParse[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文