   Java string : int conversion algorithms
  Writing in Java...I have been agaonizing over a viable (and efficient) method of converting a string (composed of characters between the values of 0 and 9 or a decimal point) to an int or double, and vice versa.Yes, this is odd! But I absolutely must capture user input in the form of a string and somehow convert the string into a number. If the string contains a decimal point, then I would like the number to be a double, else it should be an int. Don't worry about error checking, I can take care of this much. I provided the code below for a string to int conversion. Please tell me what you think, or how I could improve. But when it comes to the string to double conversion, I'm all sorts of confused, and that it where I need the most help.int stringToInt(String str){if(str == "" || str = null) return -1;int i;for(i = 0; i < str.length(); i++){if(str.charAt(i) < '0' || str.charAt(i) > '9'){if(str.charAt(i) == '.'){printError("Attempting to convert a floating-point string into an integer. Use method: double stringToInt(String)");return 0;}else{printError("Attempting to convert non-integer string into integer.");return 0;}}}int[] digitPlaces = new int[str.length()];int exponent = str.length() - 1;int result = 0;for(i = 0; i < str.length(); i++){digitPlaces = (int)(str.charAt(i) - ASCII_OFFSET);result += digitPlaces * this.power(10,exponent);exponent--;}return result;}Also, in addition to my string : int and string : double methods, I'm also looking for int : string and double : string conversion methods as well. Below is my first attempt at the int : string (it compiles but dies at runtime).String intToString(int num){String str = "";int i;int numDigits = 0;int temp;int copy = num;double factor = 10.0;//int factor = 1;double dnum = (double)num;while(dnum >= 1){dnum /= dnum;numDigits++;}temp = numDigits;char c[] = new char[numDigits];int n[] = new int[numDigits];num = copy;while(numDigits > 1){factor *= 10;numDigits--;}for(i = 0; i < temp; i++){n = (int)(num / factor);num -= n * factor;factor /= 10;c = (char)(n + ASCII_OFFSET);str += c;}return str;}
  Hi,you can make it a lot easier:String x; // contains your valueif( x.indexOf( '.' ) > -1 )    double dValue = Double.parseDouble( x );else    int iValue = Integer.parseInt( x );// And converting Int's or doubles to StringsString y = "" + iValue // or dValueMarc   ;  Right. It's surprisingly tricky, which is why it's in the standard library for you already.   