Determining the amount of digits, and storing the values in array
Author |
Message |
CooKieLord
|
Posted: Fri Oct 12, 2007 7:08 pm Post subject: Determining the amount of digits, and storing the values in array |
|
|
Situation: I want to break down an integer number into a bunch of digits, and store the value in an array.
E.g. The user enters 1 337.
The program will break it down to 1, 3, 3, 7 and store each digit in an array such that:
integerArray[0] = 1
integerArray[1] = 3
integerArray[2] = 3
integerArray[3] = 7
I understand that I can use a combination of divisions and modulo in order to determine this, but the problem I have is that I need to determine the number of digits. This is what I have, but there's are logic errors and I can't figure them out.
Java: |
public class test {
public static void main (String[] args ) {
int[] array; //Array to store broken down number
int bigNumber; //User-inout
int digit = 1; //Number of digits
int i = 0, digitMod, expo; //dunno? O_O i is for array index, digitMod and expo are test variables
bigNumber = ITI1120. readInt(); //ITI1120 is the class that they gave me to read numbers
//Block 1 - Determine the amount of digits (Need help here)
for (int d = 0; bigNumber % digit != digit; digit += 10) {
digit = d; }
array = new int[digit ];
//Block 2 - Breaking down the digits
i = digit - 1;
expo = 1;
while (i >= 0) {
digitMod = Math. pow (10, expo );
array [i ] = (bigNumber / digitMod ) % 10;
expo++;
i--;
// digitMod += 10;
};
//Block 3 - Printing the results
for (int x = 0; x < digit; x++ )
System. out. print(array [x ] + "\n");
} // end main
}//end class
|
Description: |
Scanner class given to me |
|
 Download |
Filename: |
ITI1120.java |
Filesize: |
8.89 KB |
Downloaded: |
822 Time(s) |
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
HeavenAgain

|
Posted: Fri Oct 12, 2007 7:14 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
try making the number into a string first, then its automatically in an array
just have to convert
|
|
|
|
|
 |
Euphoracle

|
Posted: Fri Oct 12, 2007 7:19 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
Well, you could do it that way, or you could do it the easy way and use a string.
Java: |
public clas test
{
public static void main (String[] args )
{
int[] digits;
int input = 0;
int digitCount = 0;
String quickCount = ""; // Hehehehe
input = ITI1120. readInt(); // I would prefer to use a long, but meh
quickCount = input + "";
digitCount = quickCount. length(); // Oh, how I love doing things the easy way some times
digits = new int[digitCount ];
for(int i = 0; i < digitCount; i++ )
{
digits [i ] = Integer. parseInt(quickCount. charAt(i )); // :p
}
for(int i = 0; i < digitCount; i++ )
System. out. println(digits [i ]);
}
} |
Of course, if you're not allowed to do that, this won't help much Sorry for any syntax errors if any, I only have access to notepad at the moment and no JDK.
|
|
|
|
|
 |
richcash
|
Posted: Fri Oct 12, 2007 7:44 pm Post subject: Re: Determining the amount of digits, and storing the values in array |
|
|
If you take the log (base 10) of a number and round down, the result will be 1 less than the number of digits in that number.
Remember, Math.log() in java.lang.Math is the natural log, so you have to use the base changing formula :
code: | Math.log(num)/Math.log(10) | will give you the log (base 10) of a number.
Do you know how to get the number of digits now? (Note, you'll have to make adjustments for 0 and negative numbers if you choose to include them).
As you said, you can use division and modulus once you know the number of digits.
|
|
|
|
|
 |
richcash
|
Posted: Sat Oct 13, 2007 12:08 pm Post subject: Re: Determining the amount of digits, and storing the values in array |
|
|
In case you're having trouble :
Java: | int num = 9846711;
int numOfDigits = (int) (Math. log (num ) / Math. log(10)) + 1; |
Post if you need help with storing the individual digits.
|
|
|
|
|
 |
wtd
|
Posted: Sat Oct 13, 2007 4:05 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
code: | class MySpecialInteger {
private int value;
public MySpecialInteger() {
this(0);
}
public MySpecialInteger(int value) {
this.value = value;
}
public int intValue() {
return value;
}
public int[] digits() {
Integer[] temp = digitsHelper(value, new java.util.ArrayDeque<Integer>());
int index = 0;
int[] digits = new int[temp.length];
for (Integer v : temp) {
digits[index++] = v;
}
return digits;
}
private static Integer[] digitsHelper(
int value,
java.util.ArrayDeque<Integer> accumulator) {
if (value > 0) {
accumulator.addFirst(value % 10);
return digitsHelper(value / 10, accumulator);
}
else {
return (Integer[])accumulator.toArray();
}
}
} |
Recursion FTW!
|
|
|
|
|
 |
richcash
|
Posted: Sat Oct 13, 2007 5:21 pm Post subject: Re: Determining the amount of digits, and storing the values in array |
|
|
I would rather write your digits() method like this.
Java: | public int[] digits () {
int[] digits = new int[(int) (Math. log(value ) / Math. log(10)) + 1];
for (int i = 0; i < digits. length; i ++ ) {
digits [i ] = (int) (value / Math. pow (10, i )) % 10;
}
return digits;
} |
But I guess thinking recursively about it is cool too.
|
|
|
|
|
 |
wtd
|
Posted: Sat Oct 13, 2007 6:03 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
You will also notice that my digitsHelper method is tail-recursive.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
CooKieLord
|
Posted: Tue Oct 16, 2007 2:18 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
Thank for all the help guys, and sorry I'm a little late in replying.
I'm mainly looking at the way richcash showed me. It's simpler for me, because I don't quite understand how to get recursion to work yet.
Now, the only problem I have is storing the broken down values in the array. With the way you're shown to me, it would place the units in [0], the 10s in [1], the 100s in [2], etc. I need the highest digit, let's say 1000s in [0], the 100s in [1], the 10s in [2] and the 1s in [3]
(I'm talking about array subscripts right? or is it called index?)
Don't tell me how to do it. I'll edit this post if I can't figure it out. I just wanted to let you guys know that I read the posts. Thanks again!
|
|
|
|
|
 |
Euphoracle

|
Posted: Tue Oct 16, 2007 2:26 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
Reverse the array:
Java: | int[] reverse(int[] source)
{
int[] reversed = new int[source.length];
// loop through, replace source into reversed backwards
// code removed, sorry, didn't read that last bit
return reversed;
} |
|
|
|
|
|
 |
CooKieLord
|
Posted: Tue Oct 16, 2007 2:36 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
Well this is what I did
Java: |
public class test {
public static void main (String[] args ) {
int[] array;
int bigNumber;
double digit;
int digint;
bigNumber = ITI1120. readInt();
digit = Math. floor(Math. log(bigNumber ) / Math. log(10)) + 1;
array = new int[(int) digit ];
int placement = (int) array. length;
for (int index= 0; index < array. length; index ++ ) {
array [index ] = (int) ((bigNumber / Math. pow (10, (placement- 1))) % 10);
placement--; };
for (int x = 0; x < array. length; x++ )
System. out. print(array [x ] + "\n");
} // end main
}//end class
|
I had two variables, one for the array and the other for the modulo. Is that what you meant by "reversing"?
|
|
|
|
|
 |
Euphoracle

|
Posted: Tue Oct 16, 2007 9:34 pm Post subject: RE:Determining the amount of digits, and storing the values in array |
|
|
Your method does work, yes.
|
|
|
|
|
 |
|
|