Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: divisibility

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

    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));
    	}
     
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: divisibility

    Quote Originally Posted by droidus View Post
    (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).
    int number = 1234;
    System.out.println(number % 10);
    System.out.println(number / 10);
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: divisibility

    Quote Originally Posted by Shaybay92 View Post
    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.

    See also: List of Unicode characters - Wikipedia, the free encyclopedia
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!