8 Replies Latest reply: Apr 8, 2007 9:10 PM by 796365

# converting an int value into an int array

I am trying to create a method that will receive an int value and determine each digit within the value. It will then return an int array of all the individual digits in value. If value = 6358, the array would store 6 in the first position, 3 in the second, 5 in the third, and 8 in the fourth.

Basically this is what I have gotten thus far, however I can't get it to compile.

class test {
public static void main(String args[]) {
System.out.print("Please enter a value of any size: ");
digits(value, 0);
}
public static void digits(int value, int i) { //this creates the size of my int array
boolean stop;
stop = true;
for (i = 0; stop; i++) {
double compare = Math.pow(10, i);
if (value > compare && value < (compare*10)) {
stop = false;
}
}
digits(value, i, 0);
}
public static int[] digits(int value, int i, int index) {
int[] digits = new int;
int temp = value;
int temp2 = value;
for (int user = (i-1); user > 0; user--) {
double power = Math.pow(10, user);
temp /=(Math.pow(10, user));
digits[index++] = temp;
temp = (value - (temp * power));
value = temp;
}
for (int j = 0; j < i; j++) {
System.out.println(digits[i]);
}
return digits;
}
}
• ###### 1. Re: converting an int value into an int array
What is your problem? Did you get any exception or error?
post that msgs
• ###### 2. Re: converting an int value into an int array
Looks like you need to use the proper formating tags
`` ``
PLACE CODE HERE!
`` ``
Just from glancing at what I could read. I see you don't have any import statements, and you don't declare any object with the name UserInput.
• ###### 3. Re: converting an int value into an int array
Post the entire error message(s), and the code that it refers to - please don't say the problem is at "line xx", we don't have your line numbers to look at.

Edit: When you post code, use the "code" button above the typing area to prevent changes and format loss - as happened.

Message was edited by:
ChuckBing
• ###### 4. Re: converting an int value into an int array
And could ou use the "code" tags: http://forum.java.sun.com/help.jspa?sec=formatting

Put [code] at the start of your code and [/code] at the end. That way your
• ###### 5. Re: converting an int value into an int array
Have a look at this
``````public class Test1
{
public static void main( String args[] ) throws Exception
{
int value = 34235;
int position = 0;
int array[] = new int[5];
while(value > 0 )
{
array[position++] = value%10;
value/= 10;
}

for(int i = 0; i < position; i++)
System.out.println(array);

}
}

Message was edited by:
qUesT_foR_knOwLeDge ``````
• ###### 6. Re: converting an int value into an int array
I found few mistakes in your code
``int[] digits = new int[];``
should be
`` int[] digits = new int[5]; ``
change size of the array according to your requirement.
``temp = (value - (temp * power));``
you should cast it to int
`` temp = (int) (value - (temp * power)); ``
``````for (int j = 0; j < i; j++) {
System.out.println(digits);
}``````
should be
``````for (int j = 0; j < i; j++) {
System.out.println(digits[j]);
}``````
change the above and compile.

bye for now
sat
• ###### 7. Re: converting an int value into an int array
Response to: lethalwire
Just from glancing at what I could read. I see you don't have any import statements, and you don't declare any object with the name UserInput.
UserInput is a class file that my course is using to allow the User to place in values for the variable it is being equated to. I don't understand what you mean by "you don't declare any object with the name UserInput." Howerver, thank you for the [[code[],[], I had no idea.

Also, I really don't get why import is so important, I've just started programming.

P.S. Thanks AnanSmriti and qUesT_foR_knOwLeDge , your input is greatly appreciated.

Message was edited by:
nndhawan
• ###### 8. Re: converting an int value into an int array
Also, I really don't get why import is so important, I've just started programming.
Then you need to do Sun's Java tutorial and learn about Java basics, then try coding. Please learn the basics before asking questions here that are covered in the tutorial.
http://java.sun.com/docs/books/tutorial/