# divisibility

• November 7th, 2011, 12:42 PM
droidus
divisibility
i am working on an assignment, where i take a number from the whole number (so 1 from 122) and check if it divisible or not. i do this for every number. if every one is divisible, it returns true. if not, return false. i have been working on this, and for some reason, when I attempt to get the first one, i am getting the wrong data. here is my code:

Code :

```public class dividesSelf {   static boolean divide(int n) {   int x = 0; int y; int z;   String number = Integer.toString(n);   for(int i = 0; i<number.length(); i++) { x = (int)number.charAt(i); }   System.out.println(x);   return false;   }   public static void main(String[] args) { System.out.println(divide(122)); }   }```
• November 7th, 2011, 01:49 PM
KevinWorkman
Re: divisibility
A char is a subset of int that can also be represented as a unicode character. The int representation of a character has little to do with which character it is (so char '9' does not map to int 9). A fun project might be to write a program that prints out the int and character values of each char.

That being said, you might want to check out the Integer class for some useful methods.
• November 8th, 2011, 08:34 PM
Junky
Re: divisibility
Quote:

Originally Posted by droidus
(so 1 from 122) and check if it divisible or not.

Divisible by what?

By the way here's a hint how to achieve this without Strings and chars (which just muddys the water as Kevin has pointed out).
Code java:

```int number = 1234; System.out.println(number % 10); System.out.println(number / 10);```
• November 11th, 2011, 02:47 AM
Shaybay92
Re: divisibility
Why didn't they use 0 - 9 ASCII for the numbers 0 - 9? Seems unnecessarily confusing to me!
So when you cast '9' to an int, you are actually getting the internal ASCII representation of '9'. From this table:
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
you will see this character is actually stored as 57 in binary. I think this is the correct explanation?
• November 12th, 2011, 10:34 AM
KevinWorkman
Re: divisibility
Quote:

Originally Posted by Shaybay92
Why didn't they use 0 - 9 ASCII for the numbers 0 - 9? Seems unnecessarily confusing to me!

Because it's an arbitrary relationship and doesn't actually mean anything. So no matter how you organize it, you're going to have a part of it that doesn't make sense to certain people.