aspose file tools*
The moose likes Beginning Java and the fly likes can't get this method to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't get this method to work

Ryan Gates
Greenhorn

Joined: Nov 07, 2011
Posts: 2
Hi all, Im writing a class that can store a 25 element char array with several methods. one method that im having trouble with is supposed to add the elements from one object of the class to another and store the result as a third object array. The array represents a 25 digit number i know that it might be easier to use integers but my professor says that if we use chars it'll run faster and use less memory.

im not sure if the trouble is with the nested if's that are supposed to carry the 1, or it its my data type conversion that's messing it up but when i try to print the result using an already working output method all i get is 25 question marks some with boxes around and some without. any help is very much appreciated=)
oh and MAX=25


John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Ryan:

Did you check the values of the chars being returned? A bunch of '?'s suggests that you're returning control characters.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
Welcome to the Ranch
Ryan Florida wrote: . . . if we use chars it'll run faster and use less memory. . . .
That sort of advice is usually misguided. When you can pay $100 for enough memory to store every person’s name on earth, you needn’t worry about 25 ints. Anyway, all the actual arithmetic is done with ints.
Why are you using Character.digit()? If you are working in decimal, you can use c - '0' where c is a char between 0 and 9. It won’t work for other number bases. You have to add the '0' after you have finished to get it back to a char.
It is confusing when you have variables called int11 etc. Get them spread out, one declaration per line, with spaces on each side of your binary operators. You never
Get a pencil and paper and see how you add to large numbers, going from right to left. Note what the value of carry is after each column.

No, forget the bl**d* chars. It is just causing you confusion. If you are worried about memory footprint, change your array to a byte[]. Or use the full width of each int and get 225 decimal digits into the length of your array!
If you have used the paper and pencil, you will see you need two new variables. There is a sum and a carry. You get from the addition to sum and carry with judicious use of the / and % operators, remembering their priorities, and putting () in the appropriate locations. You will also know whether you require the value of any of those variables for the next iteration of the addition. If you ever do, then you would have to make sure that value is still in scope and retained until your next column.

Get that lot working before you try subtraction!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Ryan Florida wrote:i know that it might be easier to use integers but my professor says that if we use chars it'll run faster and use less memory.


You are right and your professor is wrong. Okay, it might run faster (so it completes in 0.007 seconds instead of 0.009 seconds) and it might use less memory (so it uses 0.0000003 of your available memory instead of 0.0000006 of your available memory) but then the programmer has to deal with the char-to-int conversion and spend several extra hours getting it to work right. Not to mention that the faster-and-less-memory assertion is unproven and could even be false.

So yeah, you'll still have to do what your professor says, but the take-away lesson is "Premature optimization is evil". It's much better to write a simple program which might be a bit slow and pudgy than it is to write a complicated and unmaintainable program which is a tiny bit faster and slimmer.
Ryan Gates
Greenhorn

Joined: Nov 07, 2011
Posts: 2
Thank you all very much for your feedback, I just ended up rewriting it using an int array, my professor says i might get some points off for not doing how he said but i've been working on this since 10 this morning so i couldn't care less. I appreciate all the tips and insight you guys gave me. This sites pretty cool im glad i found it. I look forward to discussing future topics with y'all.

Cheers,
Ryan
 
wood burning stoves
 
subject: can't get this method to work