patternjavaModerate
Converting int value to String without using toString and parseInt method
Viewed 0 times
withoutmethodvalueparseintusingandconvertingintstringtostring
Problem
I am studying about the converting int to string and string to int without using
For example, the first converts from
Second, for converting
I am sure that t
toString and parseInt method. I already made methods. However, I don't believe that my way of doing was the best. I would like to improve these function.For example, the first converts from
String to int:public static int StringToint(String number)
int eachnumber = 0;
int intConvert = 48;
int reVal = 0;
int index;
int maxlen =number.length() - 1;
for(index = 0 ; index <= maxlen ; index++)
{
eachnumber = number.charAt(index);
eachnumber = eachnumber - intConvert;
reVal = reVal + (eachnumber * (int) Math.pow(10, maxlen - index));
}
return reVal;
}Second, for converting
int to String:public static String IntToString(int number)
{
int StringConvet = 48;
int eachDigit = number;
int afterDivide = number;
String reVal = "";
while(afterDivide >0)
{
eachDigit = afterDivide % 10;
afterDivide = afterDivide / 10;
if(eachDigit == 0)
{
reVal += "0";
}
else if(eachDigit == 1)
{
reVal += "1";
}
else if(eachDigit == 2)
{
reVal += "2";
}
else if(eachDigit == 3)
{
reVal += "3";
}
else if(eachDigit == 4)
{
reVal += "4";
}
else if(eachDigit == 5)
{
reVal += "5";
}
else if(eachDigit == 6)
{
reVal += "6";
}
else if(eachDigit == 7)
{
reVal += "7";
}
else if(eachDigit == 8)
{
reVal += "8";
}
else if(eachDigit == 9)
{
reVal += "9";
}
}
String reVal2 = "";
for(int index = reVal.length() -1 ; index >= 0 ; index--)
{
reVal2 += reVal.charAt(index);
}
return reVal2;
}I am sure that t
Solution
To improve your
Iterate digits in your
You should also use a
Since it is not homework (as per comments), I have no problems providing a code snap. It should look something like this:
Notes:
intToString() method you should consider using a StringBuilder, and specifically the method StringBuilder.append(int).Iterate digits in your
int, and for each digit you can append(eachDigit) to the StringBuilder element. This will also reduce the complexity of intToString() to \$O(n)\$ since you do not need to create a new String instance each iteration. To get a String object from the StringBuilder, use StringBuilder.toString(). Or, if you are not allowed, you can use StringBuilder.subString(0).You should also use a
StringBuilder.append() (using the same idea) to reverse the resulting string (your second loop in your code).Since it is not homework (as per comments), I have no problems providing a code snap. It should look something like this:
public static String intToString(int n) {
if (n == 0) return "0";
StringBuilder sb = new StringBuilder();
while (n > 0) {
int curr = n % 10;
n = n/10;
sb.append(curr);
}
String s = sb.substring(0);
sb = new StringBuilder();
for (int i = s.length() -1; i >= 0; i--) {
sb.append(s.charAt(i));
}
return sb.substring(0);
}Notes:
- You can also use
StringBuilder.reverse()instead of the second loop.
- In here, \$O(n)\$ means linear in the the number of digits in the input number (
nis the number of digits in the input number - not the number itself!) If you are looking for the complexity in terms of the initial number (it is \$O(\log(n))\$) since you divide your element by 10 each iterations, you have a total of \$\log_{10}(\text{number})\$ iterations for each loop, which results in \$O(\log(\text{number}))\$.
Code Snippets
public static String intToString(int n) {
if (n == 0) return "0";
StringBuilder sb = new StringBuilder();
while (n > 0) {
int curr = n % 10;
n = n/10;
sb.append(curr);
}
String s = sb.substring(0);
sb = new StringBuilder();
for (int i = s.length() -1; i >= 0; i--) {
sb.append(s.charAt(i));
}
return sb.substring(0);
}Context
StackExchange Code Review Q#10351, answer score: 12
Revisions (0)
No revisions yet.