
-----------------------------------
Epoch.
Tue Mar 28, 2006 8:56 am

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.

-----------------------------------
Andy
Tue Mar 28, 2006 12:19 pm


-----------------------------------
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
Tue Mar 28, 2006 1:32 pm


-----------------------------------
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.

-----------------------------------
wtd
Tue Mar 28, 2006 1:41 pm


-----------------------------------
It's easy enough to do with integers if you understand modulus and recursion.

-----------------------------------
Epoch.
Wed Mar 29, 2006 8:16 am


-----------------------------------
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".

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.

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
Wed Mar 29, 2006 9:15 am


-----------------------------------
then you're doing something wrong.. post us the code you used to convert from integer to string

-----------------------------------
HellblazerX
Wed Mar 29, 2006 8:51 pm


-----------------------------------
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.

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.
Thu Mar 30, 2006 8:44 am


-----------------------------------
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 :( 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. :)

-----------------------------------
md
Thu Mar 30, 2006 9:21 am


-----------------------------------
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
Thu Mar 30, 2006 5:56 pm


-----------------------------------
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
Thu Mar 30, 2006 6:41 pm


-----------------------------------
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
Thu Mar 30, 2006 8:09 pm


-----------------------------------
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
Thu Mar 30, 2006 10:09 pm


-----------------------------------
Actually Cornflakes is not.  He would have to use "new" to do that.

-----------------------------------
Epoch.
Fri Mar 31, 2006 8:14 am


-----------------------------------
*sigh* For some reason neither of those two methods work for me... here's the code and the *new* given errors.

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);

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 :(

Thanks again for you help. :)

-----------------------------------
wtd
Fri Mar 31, 2006 2:54 pm


-----------------------------------
Works fine for me.

scala> java.lang.Integer.toString(42)
line5: java.lang.String = 42
