Home:ALL Converter>Java: Converting a String into an Int recursively?

Java: Converting a String into an Int recursively?

Ask Time:2014-02-04T07:08:39         Author:user1908627

Json Formatter

I'm trying to create a Java program that converts a String into an Integer recursively. This is currently what I have but it gives me an error, "Exception in thread "main" java.lang.NumberFormatException". The method is supposed to take in a number in the form of a string then iterate through each position. Through each iteration it turns the single number into a integer and adds it to x. By the end of it x is suppose to have the String number in integer form.

import 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));*/ } }

Author:user1908627,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21539734/java-converting-a-string-into-an-int-recursively
Ashot Karakhanyan :

Add \n\ninteger = Integer.parseInt(numb.substring(index, index + 1)); //\nindex++;\n\n\nInstead of:\n\ninteger=Integer.parseInt(\"numb.charAt(index)\"); \n\n\nAnd remove ++ of index++ from return intconvert(numb, index++, times * 10); its not increase passed index.",
2014-02-03T23:17:50
Shlomi :

Numbers are sequential in their ascii values, so in order to turn your char into an int, you could simply do:\n\nint integer = numb.charAt(index) - '0';\n\n\nall that is left is to ensure that integer is between bounds and your function should work just fine.\n\nby the way, I would remove the static global variables. If you simply pass them as parameters instead, your solution will be \"pure\", as in side-effect free or referentially transparent",
2014-02-03T23:26:46
pedromss :

Even if the method was correct, i.e:\n\npublic static int intconvert(String numb, int index, int times) {\n if (index == numb.length()) { return x; }\n integer = Integer.parseInt(String.valueOf(numb.charAt(index))); //\n x += integer * times; // add int and multiply it\n return intconvert(numb, index++, times * 10); //\n\n }\n\n\nYou'll still get an StackOverFlow exception, because you of the way you increment your x, it will never enter the stopping condition.\n\nIf I understood what you wanted to do, the solution is:\n\npublic class Cenas {\n\n public static int x = 0;\n public static int integer;\n\n public static int intconvert(String numb, int index, int times) {\n integer = Integer.parseInt(Character.toString(numb.charAt(index))); //\n x += integer * times; // add int and multiply it\n if (index == 0) { return x; }\n return intconvert(numb, --index, times * 10); //\n\n }\n\n public static void main(String[] args) {\n\n Scanner scan = new Scanner(System.in);\n System.out.print(\"Enter the String digit: \");\n String number = scan.nextLine();\n System.out.println(intconvert(number, number.length() - 1, 1));\n}\n\n\nStart at the algarism with the less weight and work your way to the beggining index, also you were missing the print statement at your main call.\n\nBecause you are incrementing your \"times\" 10 times by each iteration you must start ate the last index of the string.\n\nExample:\n123 = 1 * 100 + 2 * 10 + 3 * 1\n\nYour problem was not recursion but the algorithm you were using.",
2014-02-03T23:15:47
Christian Tapia :

The line\n\ninteger=Integer.parseInt(numb.charAt(index));\n\n\nwon't work, because charAt() returns a char, and parseInt expects a String. Try converting that char into a String with Character.toString(c):\n\nInteger.parseInt(Character.toString(numb.charAt(index)))\n",
2014-02-03T23:13:56
yy