Convert an integer to an array of digits
The immediate problem is due to you using <= temp.length()
instead of < temp.length()
. However, you can achieve this a lot more simply. Even if you use the string approach, you can use:
String temp = Integer.toString(guess);
int[] newGuess = new int[temp.length()];
for (int i = 0; i < temp.length(); i++)
{
newGuess[i] = temp.charAt(i) - '0';
}
You need to make the same change to use < newGuess.length()
when printing out the content too - otherwise for an array of length 4 (which has valid indexes 0, 1, 2, 3) you'll try to use newGuess[4]
. The vast majority of for
loops I write use <
in the condition, rather than <=
.
How to convert an integer into an array of digits
I'd go with
var arr = n.toString(10).replace(/\D/g, '0').split('').map(Number);
You can omit the replace
if you are sure that n
has no decimals.
Converting an integer to an array of digits
Off the top of my head:
int i = 123;
var digits = i.ToString().Select(t=>int.Parse(t.ToString())).ToArray();
Is there an easy way to turn an int into an array of ints of each digit?
public Stack<int> NumbersIn(int value)
{
if (value == 0) return new Stack<int>();
var numbers = NumbersIn(value / 10);
numbers.Push(value % 10);
return numbers;
}
var numbers = NumbersIn(987654321).ToArray();
Alternative without recursion:
public int[] NumbersIn(int value)
{
var numbers = new Stack<int>();
for(; value > 0; value /= 10)
numbers.Push(value % 10);
return numbers.ToArray();
}
convert an integer number into an array
This would work for numbers >= 0
#include <math.h>
char * convertNumberIntoArray(unsigned int number) {
int length = (int)floor(log10((float)number)) + 1;
char * arr = new char[length];
int i = 0;
do {
arr[i] = number % 10;
number /= 10;
i++;
} while (number != 0);
return arr;
}
EDIT: Just a little bit more C style but more cryptic.
#include <math.h>
char * convertNumberIntoArray(unsigned int number) {
unsigned int length = (int)(log10((float)number)) + 1;
char * arr = (char *) malloc(length * sizeof(char)), * curr = arr;
do {
*curr++ = number % 10;
number /= 10;
} while (number != 0);
return arr;
}
How to put an integer to an array of digits
First calculate no of digits
int count = 0;
int n = number;
while (n != 0)
{
n /= 10;
cout++;
}
Now intialize the array and assign the size:
if(count!=0){
int numberArray[count];
count = 0;
n = number;
while (n != 0){
numberArray[count] = n % 10;
n /= 10;
count++;
}
}
Converting integer into array of digits
Just using something like this:
int n = 544; // your number (this value will Change so you might want a copy)
int i = 0; // the array index
char a[256]; // the array
while (n) { // loop till there's nothing left
a[i++] = n % 10; // assign the last digit
n /= 10; // "right shift" the number
}
Note that this will result in returning the numbers in reverse order. This can easily be changed by modifying the initial value of i
as well as the increment/decrement based on how you'd like to determine to length of the value.
(Brett Hale) I hope the poster doesn't mind, but I thought I'd add a code snippet I use for this case, since it's not easy to correctly determine the number of decimal digits prior to conversion:
{
char *df = a, *dr = a + i - 1;
int j = i >> 1;
while (j--)
{
char di = *df, dj = *dr;
*df++ = dj, *dr-- = di; /* (exchange) */
}
}
How do I separate an integer into separate digits in an array in JavaScript?
Why not just do this?
var n = 123456789;
var digits = (""+n).split("");
Related Topics
C# List of Objects, How to Get the Sum of a Property
How to Pass a Value from a Child Back to the Parent Form
Run Single Instance of an Application Using Mutex
How to Detect If Console.In (Stdin) Has Been Redirected
How to Check Whether a String Is a Valid Http Url
Linq - Left Join, Group By, and Count
Deep Null Checking, Is There a Better Way
How to Protect My .Net Assemblies from Decompilation
How to Get the Current User in ASP.NET MVC
How Might I Schedule a C# Windows Service to Perform a Task Daily
Xml Serialization - Hide Null Values
{"<User Xmlns=''> Was Not Expected.} Deserializing Twitter Xml
How to Make Sure That String Is Valid JSON Using JSON.Net
Using JSONconvert.Deserializeobject to Deserialize JSON to a C# Poco Class