Error integer too long

Hi,

I'm taking a Java class currently, so please forgive me if I make any stupid mistakes. My assignment was to make a UPC checker and we are supposed to code in Bluej.

//Code thus far

import chn.util.*;

public class UPCChecker

{

private long checkdigit;

private int[] numArray = new int[11];

public long getDigit(long code)

{

for(int i=10; i>=0; i--)

{

numArray = (int)(code%10);

code = code/10;

}

long sum = numArray[0] + numArray[2] + numArray[4] + numArray[6] + numArray[8] + numArray[10];

sum *= 3;

long sum2 = numArray[1] + numArray[3] + numArray[5] + numArray[7] + numArray[9];

sum += sum2;

checkdigit = sum%10;

if(checkdigit != 0)

checkdigit = 10 - checkdigit;

return checkdigit;

}

public boolean isValid()

{

boolean valid;

if (checkdigit == numArray[11])

{

valid = true;

}

else

{

valid = false;

}

return valid;

}

}

//End Code

When I create an instance, run it in Bluej (it automatically provides the box for input), and type in an example UPC code (12 digits), it says "Error: integer number too large". When I type in 9 digits, it works though. I would appreciate any advice and help.

Thanks,

miscreant.

[1384 byte] By [miscreanta] at [2007-10-3 4:35:50]
# 1
Next time you post your source code stick it between [code] ... [/code]tags; now your code is unreadable.kind regards,Jos
JosAHa at 2007-7-14 22:39:30 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks for the advice, I'll repost it the correct way:

import chn.util.*;

public class UPCChecker

{

private long checkdigit;

private int[] numArray = new int[11];

public long getDigit(long code)

{

for(int i=10; i>=0; i--)

{

numArray = (int)(code%10);

code = code/10;

}

long sum = numArray[0] + numArray[2] + numArray[4] + numArray[6] + numArray[8] + numArray[10];

sum *= 3;

long sum2 = numArray[1] + numArray[3] + numArray[5] + numArray[7] + numArray[9];

sum += sum2;

checkdigit = sum%10;

if(checkdigit != 0)

checkdigit = 10 - checkdigit;

return checkdigit;

}

public boolean isValid()

{

boolean valid;

if (checkdigit == numArray[11])

{

valid = true;

}

else

{

valid = false;

}

return valid;

}

}

*It works when I type in 10 digits, not 9.

Message was edited by:

miscreant

miscreanta at 2007-7-14 22:39:30 > top of Java-index,Java Essentials,New To Java...
# 3

> Thanks for the advice, I'll repost it the correct way:

Good, but now your code doesn't even compile. My guess is that you

copied and pasted the source from your original post and put it between

those tags. This what it reads like now:> import chn.util.*;

> public class UPCChecker

> {

> private long checkdigit;

> private int[] numArray = new int[11];

> public long getDigit(long code)

> {

>

>

> for(int i=10; i>=0; i--)

> {

> numArray = (int)(code%10);

No need to go any further: you cant assign an int value to an int array.

Most likely you'll find me a nasty nitpicker but that's what programming

is all about: the design is the overall structure of it all, the implementation

is about those gory details which should at least be readable for the

audience you're writing for in this forum.

kind regards,

Jos

JosAHa at 2007-7-14 22:39:30 > top of Java-index,Java Essentials,New To Java...
# 4

> private int[] numArray = new int[11];

...

> if (checkdigit == numArray[11])

Just to help you out a bit: when an array contains n elements, the

elements are indexed as 0, 1, 2, ... n-1

kind regards,

Jos

JosAHa at 2007-7-14 22:39:30 > top of Java-index,Java Essentials,New To Java...
# 5

Are you sure this compiles?

for(int i=10; i>=0; i--)

{

numArray = (int)(code%10); // <-- This is type mismatch, int to int[]

code = code/10;

}

Also

public boolean isValid()

{

boolean valid;

if (checkdigit == numArray[11]) //<- numArray[11] is OOB

{

valid = true;

}

else

{

valid = false;

}

return valid;

}

tanmay.sinhaa at 2007-7-14 22:39:30 > top of Java-index,Java Essentials,New To Java...