Results 1 to 4 of 4
  1. #1
    sweetjava is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default can anyone tell my why this program bugs out when i enter more then 10 digits?

    I wrote this program as an exercise, and although it appears to work fine, if I enter more then 10 digits it says illegal numeric format.

    Can someone please tell me why it is doing this?

    Java Code:
    import acm.program.ConsoleProgram;
    
    public class countdigits extends ConsoleProgram {
    
    public void run () {
    	println ("this program counts the digits in a given number");
    	int x = readInt ("enter number:");
    	int y = 0;
    println ("the number of digits in this number is: " + countInt(x, y));
    
    }
    
    
    	public int countInt (int x, int y) {
    		
    		int count = y;
    		while (x > 0) {
    			x /= 10;
    			count++;
    		
    		}
    		return count;

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Ask yourself the question: "what is the largest 'int' value there can be?" =)

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    An Integer's largest value can be ( 2^(31) ) - 1

    You can get this value from
    Java Code:
    Integer.MAX_VALUE
    If you want to be able to hold something larger, have a look at java.math.BigInteger


    Mr. Beans

  4. #4
    sweetjava is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default

    That makes perfect sense to me, thanks guys, I figured it was somthing obvious!

Similar Threads

  1. Replies: 2
    Last Post: 07-13-2009, 04:45 AM
  2. how do you bypass the enter(/n) key in a program?
    By mtwisd in forum New To Java
    Replies: 1
    Last Post: 04-24-2009, 08:50 AM
  3. My first Java project, encountering bugs
    By beatzz in forum New To Java
    Replies: 1
    Last Post: 03-17-2009, 08:32 PM
  4. sum of digits depreciation
    By jleas in forum New To Java
    Replies: 13
    Last Post: 11-09-2008, 01:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •