java programming question

iskim86

Banned
Jul 6, 2001
1,802
0
0
www.isaackim.org
ok so I have a bunch of numbers like this...

1421
40104
321
32
etc

how do I put them in an array and shift it to the right side of the array so it looks like this:

00000000001421
00000000040104
etc

the length of the whole array is 25. so if there are 4 digits in the input number, there should be 21 zeros in front of it.

they need to add up correctly when done the math.

so far I got int[] total = new int[25]

i was thinking of setting the array to 25 blocks and making a for-loop so that I can add zeros in front of the numbers until the 'total.length' of array reaches 25

so maybe it should start out with something like:

for (int i = 0; i < 25; i++) {

but I only have the idea down, I have no idea how I should write this out in java programming language.

thanks for any help.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Convert the integer to a string, append the appropriate amount of 0's to the front of it using a for loop, parse the string back to an integer.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
Let num = your number, precondition is it is 25 or less digits long. i'm fuzzy on java syntax but will give it a try. i think you can see the concept you need to do from this.


int[] total = new int[25];

numstr = toString(num); <- turn the number into a string.
int y = 0;

for(int x = 26 - numstr.length(); x < 26; x++)
{

total[x] = numstr.charAt(y);
y++;

}

for(int z = 0; z < total.length() - numstr.length(); z++)
{

total[z] = '0';

}

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Do you want an array of strings, or an array of integers. The ONLY time that having all those zeros padding your numbers is useful is if you're trying to make them line up on the screen. You don't need a bunch of zeros just to add them up. Nevermind that a 32 bit integer can't have more than 10 digits.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: tfinch2
...parse the string back to an integer.

Which will remove all your useless zeros that you just added. Do you even have the slightest idea of how an integer is represented in memory?
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: notfred
Originally posted by: tfinch2
...parse the string back to an integer.

Which will remove all your useless zeros that you just added. Do you even have the slightest idea of how an integer is represented in memory?

Exactly.

What you might want to do if you want to keep all those intergers is just an array of ints (or bytes) and just store one digit per place Or, you can store it as a string and parse it when you want the value. Otherwise, those zeros are all going to be removed.
 

iskim86

Banned
Jul 6, 2001
1,802
0
0
www.isaackim.org
ok the thing is, I have to use arrays to organize the numbers so that the zeros come in the front and the input numbers to go in the back, with the array having a maximum of 25 blocks. when doing the calculation, the zeros must be present so that I can do calculations vertically with the digits all lined up. when I display on screen, the zeros should be gone for easy reading.

 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: iskim86
ok the thing is, I have to use arrays to organize the numbers so that the zeros come in the front and the input numbers to go in the back, with the array having a maximum of 25 blocks. when doing the calculation, the zeros must be present so that I can do calculations vertically with the digits all lined up. when I display on screen, the zeros should be gone for easy reading.
You aren't making a lot of sense. Whether you do:

00007
+00008

or 7+8, the answer is still 15. The only time that the zeroes would even show up is on display, yet you want to remove them when you display it....
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: iskim86
ok the thing is, I have to use arrays to organize the numbers so that the zeros come in the front and the input numbers to go in the back, with the array having a maximum of 25 blocks. when doing the calculation, the zeros must be present so that I can do calculations vertically with the digits all lined up. when I display on screen, the zeros should be gone for easy reading.

What kind of calculations are you doing if you don't mind?

 

znaps

Senior member
Jan 15, 2004
414
0
0
It might be an exercise in representing numbers greater than that provided by a language's own numeric datatypes. I remember doing something similar at uni using Modula 2.

Then again, if he's using ints, that ain't it.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Here's my solution... I'm gonna assume you start with a single number, say 12345, and want to end up with an array of ints like so: 0, 0, 0, .... 0, 1, 2, 3, 4, 5
 

iskim86

Banned
Jul 6, 2001
1,802
0
0
www.isaackim.org
Originally posted by: amdfanboy
Originally posted by: iskim86
ok the thing is, I have to use arrays to organize the numbers so that the zeros come in the front and the input numbers to go in the back, with the array having a maximum of 25 blocks. when doing the calculation, the zeros must be present so that I can do calculations vertically with the digits all lined up. when I display on screen, the zeros should be gone for easy reading.

What kind of calculations are you doing if you don't mind?

ok......

say I'm doing this.

32133213241 5142313

the above numbers are part of sum.txt, an external file where the program gets numbers.

the program is supposed to add each row of numbers.

so for the above, it'll display:
32133213241 + 5142313 = {answer goes here}

I need to put the zeros in front of them so the .length of the array reaches 25 so that it lines up all the columns correctly to do vertical addition. right now, I'm missing the part where it's supposed to insert the zeros in the front of the numbers so for the above equation, I'm just getting a 0. so I need to add zeros in my array of numbers, make the program do the math, then delete the 0s for it to display neatly.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Oh, upon having been sneak-posted 4 times while writing that last one I have another thought. Would it help you to know the number of digits that your int takes up in decimal? Like 1234 would be 4 and 34 would be 2? You can get that with the base 10 log of your number (but don't ask me to remember if you have to round down or up...
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: iskim86
Originally posted by: amdfanboy
Originally posted by: iskim86
ok the thing is, I have to use arrays to organize the numbers so that the zeros come in the front and the input numbers to go in the back, with the array having a maximum of 25 blocks. when doing the calculation, the zeros must be present so that I can do calculations vertically with the digits all lined up. when I display on screen, the zeros should be gone for easy reading.

What kind of calculations are you doing if you don't mind?

ok......

say I'm doing this.

32133213241 5142313

the above numbers are part of sum.txt, an external file where the program gets numbers.

the program is supposed to add each row of numbers.

so for the above, it'll display:
32133213241 + 5142313 = {answer goes here}

I need to put the zeros in front of them so the .length of the array reaches 25 so that it lines up all the columns correctly to do vertical addition. right now, I'm missing the part where it's supposed to insert the zeros in the front of the numbers so for the above equation, I'm just getting a 0. so I need to add zeros in my array of numbers, make the program do the math, then delete the 0s for it to display neatly.

Why do you need to use verticle addition for that? Can't you just say var3 = var1 + var2
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
You don't need a bunch of extraneous zeros to add numbers.

2 + 71 = 73

02 + 71 = 73

00002 + 071 = 73
 

iskim86

Banned
Jul 6, 2001
1,802
0
0
www.isaackim.org
hmmmm i haven't gone that far in my class so I wouldn't know. I'm just 2 months into this class so far, and I'm not liking it. programming is definately not my thing
 

znaps

Senior member
Jan 15, 2004
414
0
0
hah, I was right go me.

Originally posted by: znaps
It might be an exercise in representing numbers greater than that provided by a language's own numeric datatypes. I remember doing something similar at uni using Modula 2.

Then again, if he's using ints, that ain't it.

 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: notfred
Originally posted by: tfinch2
...parse the string back to an integer.

Which will remove all your useless zeros that you just added. Do you even have the slightest idea of how an integer is represented in memory?

Actually I do. I just spit out a quick answer and didn't even think about it.
 
Aug 3, 2004
35
0
0
How about this... To find out the number of digits in your number take the log of it, add 1 and truncate the decimal. Once you have the number of digits "length", you can just use a for loop. Here's some pseudo code
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |