Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fun with arrays!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Epoch.




PostPosted: Tue Mar 28, 2006 8:56 am   Post subject: Fun with arrays!

Hi, I was wondering if there was a way to seperate the digits in an integer into an array. I noticed it can be done with Strings, but I haven't seen anything hinting towards integers.

The reason I am asking is I need to write a program to take in a nine digit number and then do different calculations using the seperate digits. For example, step one requires me to double the second, fourth, sixth and eighth digits and then add them.

Any help is appreciated greatly.
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Tue Mar 28, 2006 12:19 pm   Post subject: (No subject)

why not convert the integer into a string then break it down? or you can write your own method to extract the last digit add it to your array, then delete the integer by 10
Dan




PostPosted: Tue Mar 28, 2006 1:32 pm   Post subject: (No subject)

Why converter it to an int to begin with? You are inputing it as a string so leave it as a string, use toCharArray and then boom you have an array of chars repersting the ascii of the number. Then you could either leave them as chars and uses an offset or convert them to ints as needed.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Tue Mar 28, 2006 1:41 pm   Post subject: (No subject)

It's easy enough to do with integers if you understand modulus and recursion.
Epoch.




PostPosted: Wed Mar 29, 2006 8:16 am   Post subject: (No subject)

Andy wrote:
why not convert the integer into a string then break it down? or you can write your own method to extract the last digit add it to your array, then delete the integer by 10


It won't let me convert an int value to a String value, it gives me an error reading "inconvertible types".

Hacker Dan wrote:
Why converter it to an int to begin with? You are inputing it as a string so leave it as a string, use toCharArray and then boom you have an array of chars repersting the ascii of the number. Then you could either leave them as chars and uses an offset or convert them to ints as needed.


I'm taking it in as an int, not a String - but I see what you're getting at. I'll give it a shot.

wtd wrote:
It's easy enough to do with integers if you understand modulus and recursion.


I know how to use modulus, but I have never seen recursion. I've only been programming for about two months in a stupid grade 12 independant course since the normal teacher in on maternity leave. So, I'm not given the material as fast since I go at my own pace without set deadlines. The whole thing is dumb but I need the cred to get into my courses in college.

Again, your help is appreciated.
Andy




PostPosted: Wed Mar 29, 2006 9:15 am   Post subject: (No subject)

then you're doing something wrong.. post us the code you used to convert from integer to string
HellblazerX




PostPosted: Wed Mar 29, 2006 8:51 pm   Post subject: (No subject)

Epoch. wrote:
It won't let me convert an int value to a String value, it gives me an error reading "inconvertible types".


it sounds like you tried to typecast the int into String, which you cant, because you can't typecast from a primitive to an object. You'll need to use the toString () method.

Epoch. wrote:
I'm taking it in as an int, not a String - but I see what you're getting at. I'll give it a shot.


I'm pretty sure all input comes in as String to begin with. How are you taking it in as an int?
Epoch.




PostPosted: Thu Mar 30, 2006 8:44 am   Post subject: (No subject)

code:
class SIN

{
        public static void main (String[] args)
        {
                //variable declarations
                int number;
                int stepOne, stepTwo, stepThree, stepFour, stepFive;
                String strNumber;
                char[] sinArray;
                sinArray = new char[9];

                //request input
                System.out.print("Please input a 9-digit SIN number: ");
                number = In.getInt();

                //while statements make sure the number is nine digits
                while (number > 999999999)
                {
                        System.out.println("Sorry, invalid number.");
                        System.out.println("Please input a new number: ");
                        number = In.getInt();
                }

                while (number < 100000000)
                {
                        System.out.println("Sorry, invalid number.");
                        System.out.println("Please input a new number: ");
                        number = In.getInt();
                }

                //make the given number a string in order to put it into an array
                strNumber = toString(int number);
                //put the digit values into a char array
                sinArray = strNumber.toCharArray();

                //convert the digit values in the array from unicode to integer
                //in order to do calculations
                sinArray[1] = (int)(sinArray[1]);
                sinArray[2] = (int)(sinArray[2]);
                sinArray[3] = (int)(sinArray[3]);
                sinArray[4] = (int)(sinArray[4]);
                sinArray[5] = (int)(sinArray[5]);
                sinArray[6] = (int)(sinArray[6]);
                sinArray[7] = (int)(sinArray[7]);
                sinArray[8] = (int)(sinArray[8]);
                sinArray[9] = (int)(sinArray[9]);

        }
}


I left out the final calculations because they're fairly straightforward arithmetic, no problems there.

The toString section is now the only thing giving me errors now Sad I'm guessing I've implemented it wrong? The errors are:

U:\Progs\Chapter 4\SIN.java:37: '.class' expected
strNumber = toString(int number);
^
U:\Progs\Chapter 4\SIN.java:37: ')' expected
strNumber = toString(int number);

Thanks again for your help so far. Smile
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Thu Mar 30, 2006 9:21 am   Post subject: (No subject)

I think it should be Int(number).toString()

You should also take a look at your code for making sure the number is 9 digits... what happens if I enter 1000000001 so it goes past the first loop, and then 3 inside the second?
HellblazerX




PostPosted: Thu Mar 30, 2006 5:56 pm   Post subject: (No subject)

really, I thought it was Integer.toString (number). But then again, I'm using a really outdated Java compiler, so what do I know?
md




PostPosted: Thu Mar 30, 2006 6:41 pm   Post subject: (No subject)

It very well could be Integer(number).toString()... I was guessing from a mediocre memory of Java. The logic error on the other hand is pretty glairing.
Andy




PostPosted: Thu Mar 30, 2006 8:09 pm   Post subject: (No subject)

both works. hellblazer is simply using an static method of the Integer class, and cornflake is actually creating an Integer object and then converting it to String
wtd




PostPosted: Thu Mar 30, 2006 10:09 pm   Post subject: (No subject)

Actually Cornflakes is not. He would have to use "new" to do that.
Epoch.




PostPosted: Fri Mar 31, 2006 8:14 am   Post subject: (No subject)

*sigh* For some reason neither of those two methods work for me... here's the code and the *new* given errors.

code:
strNumber = Integer.toString(number);


U:\Progs\Chapter 4\SIN.java:37: toString() in java.lang.Object cannot be applied to (int)
strNumber = Integer.toString(number);

code:
strNumber = Integer(number).toString();


U:\Progs\Chapter 4\SIN.java:37: cannot find symbol
symbol : method Integer(int)
location: class SIN
strNumber = Integer(number).toString();

I'm going to press on to the next chapter of work and come back to this one, but any further assistance will be greatly appreciated as I cannot find anything about this anywhere Sad

Thanks again for you help. Smile
wtd




PostPosted: Fri Mar 31, 2006 2:54 pm   Post subject: (No subject)

Works fine for me.

code:
scala> java.lang.Integer.toString(42)
line5: java.lang.String = 42
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: