1

i have an error while trying to insert 09 in my ArrayList's add method how to resolve it when i google for it its give me solution like its a octal no and i need to chage this to decimal no how do i do this please any one help me ...

import java.util.*;
class MyArrayList{
   public static void main(String args[]){
        ArrayList al=new ArrayList();
        al.add(new Integer(09));
        System.out.println(al);
   }
}


output:MyArrayList.java:5:error integer too large:09
Jagdish
  • 129
  • 1
  • 8

4 Answers4

2

Because values leading 0 are considered to be octal numbers. And octal numbers range from 0-7. if it considers your given value as octal , even though it is not valid octal number.

Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
1

From the JavaDoc of the Integer constructor:

Constructs a newly allocated Integer object that represents the int value indicated by the String parameter. The string is converted to an int value in exactly the manner used by the parseInt method for radix 10.

So in case you wanted to insert 9 (base 10) with a leading 0, omit the '0'. If your number is indeed an octal number, create the Integer object by parsing:

Integer outputDecimal = Integer.parseInt(inputHex, 8);
hotzst
  • 7,238
  • 9
  • 41
  • 64
1

This has nothing to do with the ArrayList. The problem is with 09 alone, for example a more characteristic example of the same problem:

int num = 09;

Because numeric literals starting with 0 are treated as octal numbers, and 9 is too big for that, as octal digits are from 0 to 7. So for the same reason 08 would give the same error, and 07 is fine.

janos
  • 120,954
  • 29
  • 226
  • 236
1

Numbers starting with 0 in Java are considered as octal numbers and 9 is not an octal digit(only 0-7 are). Numbers starting with 0x in Java are considered hexadecimal(the digits are 0-9 and A to F). From Java SE 7, Numbers starting with 0b are considered binary(the digits are 0 and 1).

Hence your number 09 is recognised as octal and 9 doesn't exist in octal digits.

Anoop Garlapati
  • 257
  • 2
  • 14