【问题标题】:Java: Converting a String into an Int recursively?Java:递归地将字符串转换为 Int?
【发布时间】:2014-02-27 16:04:11
【问题描述】:

我正在尝试创建一个 Java 程序,它可以递归地将字符串转换为整数。这是我目前所拥有的,但它给了我一个错误,“线程“main”java.lang.NumberFormatException中的异常”。该方法应该以字符串的形式接收一个数字,然后遍历每个位置。通过每次迭代,它将单个数字转换为整数并将其添加到 x。最后,假设 x 具有整数形式的字符串数。

导入 java.util.Scanner; public class Problem{ public static int x=0; public static int integer; public static int intconvert(String numb,int index,int times){ if(index==numb.length()){ return x; } else{ integer=Integer.parseInt("numb.charAt(index)"); // x+=integer*times; //add int and multiply it return intconvert(numb, index++, times*10); // } } public static void main(String[] args){ Scanner scan=new Scanner(System.in); System.out.print("Enter the String digit: "); String number=scan.nextLine(); intconvert(number, 0, 1); /* System.out.println(number.charAt(0)); System.out.println(number.charAt(1)); System.out.println(number.charAt(2));*/ } }

【问题讨论】:

  • 您正在传递一个不可解析的字符串。删除 numb.charAt(index) 周围的引号。
  • 为什么每个人都突然实现所有东西的递归版本,这些东西可以通过简单的迭代更好地实现? (我知道,现在还处于这个学期的早期,他们刚刚了解了递归,并认为它很酷,但不明白什么时候合适,什么时候不合适。不过:grrrrrrr。结束咆哮。)
  • 我被曝光了。这只是一个帮助我理解递归的练习。这段代码确实没有实际用途,我相信简单的迭代可以轻松解决这个问题。

标签: java string recursion int


【解决方案1】:

尝试改变

integer=Integer.parseInt("numb.charAt(index)");

进入

integer=Integer.parseInt(numb.substring(index, index + 1));

原行尝试在字符串"numb.charAt(index)" 中查找一个数字,该字符串不包含任何数字。

另外,将index++ 更改为index + 1 甚至++index,因为index++ 在这种情况下不起作用(它只会在index 使用后递增,并且只是在超出范围之前。

【讨论】:

  • numb.charAt(index)) 返回一个字符
【解决方案2】:

线

integer=Integer.parseInt(numb.charAt(index));

不起作用,因为charAt() 返回一个char,而parseInt 需要一个String。尝试使用Character.toString(c)char 转换为String

Integer.parseInt(Character.toString(numb.charAt(index)))

【讨论】:

    【解决方案3】:

    即使方法正确,即:

    public static int intconvert(String numb, int index, int times) {
            if (index == numb.length()) { return x; }
            integer = Integer.parseInt(String.valueOf(numb.charAt(index))); //
            x += integer * times; // add int and multiply it
            return intconvert(numb, index++, times * 10); //
    
        }
    

    你仍然会得到一个 StackOverFlow 异常,因为你增加 x 的方式,它永远不会进入停止条件。

    如果我理解你想要做什么,解决方案是:

    public class Cenas {
    
        public static int x = 0;
        public static int integer;
    
        public static int intconvert(String numb, int index, int times) {
            integer = Integer.parseInt(Character.toString(numb.charAt(index))); //
            x += integer * times; // add int and multiply it
            if (index == 0) { return x; }
            return intconvert(numb, --index, times * 10); //
    
        }
    
        public static void main(String[] args) {
    
            Scanner scan = new Scanner(System.in);
            System.out.print("Enter the String digit: ");
            String number = scan.nextLine();
            System.out.println(intconvert(number, number.length() - 1, 1));
    }
    

    从重量较轻的 algarism 开始,然后按自己的方式到达起始索引,而且您在主调用时错过了 print 语句。

    因为每次迭代都会将“倍”增加 10 次,所以必须从字符串的最后一个索引开始。

    示例: 123 = 1 * 100 + 2 * 10 + 3 * 1

    您的问题不是递归,而是您使用的算法。

    【讨论】:

    • 感谢您的提醒。有什么增加 x 的建议吗?
    • 我必须了解该方法的目的,因为现在我不明白为什么不调用 Integer.parseInt(string_that_represents_a_number),您描述问题的方式让我认为不需要递归,a简单的循环就可以了。
    • 真的没有理由。这只是我书中的一个练习,可以帮助我通过练习题理解递归。
    • 我了解您要完成的工作吗?
    • 是的,你成功了!谢谢。
    【解决方案4】:

    添加

    integer = Integer.parseInt(numb.substring(index, index +  1)); //
    index++;
    

    代替:

    integer=Integer.parseInt("numb.charAt(index)"); 
    

    并从return intconvert(numb, index++, times * 10); 中删除index++++,其未通过index 增加。

    【讨论】:

      【解决方案5】:

      数字的 ascii 值是连续的,因此要将您的 char 转换为 int,您只需这样做:

      int integer = numb.charAt(index) - '0';
      

      剩下的就是确保integer 在界限之间,并且您的函数应该可以正常工作。

      顺便说一句,我会删除静态全局变量。如果您只是将它们作为参数传递,您的解决方案将是“纯”的,如无副作用或referentially transparent

      【讨论】:

        猜你喜欢
        • 2013-11-03
        • 2019-04-03
        • 2016-07-07
        • 2021-08-01
        • 1970-01-01
        • 2020-02-17
        • 2014-01-25
        • 2011-10-02
        相关资源
        最近更新 更多