Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Using "08" and "09" in an array

Tags:

java

arrays

How can I store 08 and 09 into an int array? I understand that they cannot due to binary characteristics and have tried both...

0b08, 0b09 ... and ... 0B08, 0B09 with no luck.

The following line of code is ideally what I'd like to have:

final int[] monthValidDosInputs = {00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12};

Here is the error...

ConvertDate.java:15: error: integer number too large: 08
ConvertDate.java:15: error: integer number too large: 09

Thanks!

like image 414
ewbrowning Avatar asked Apr 22 '14 05:04

ewbrowning


People also ask

How do you write numbers in an array in Java?

Each element in the array is accessed via its index. The index begins with 0 and ends at (total array size)-1. All the elements of array can be accessed using Java for Loop. // accessing the elements of the specified array for (int i = 0; i < arr.length; i++) System.out.println("Element at index " + i + " : "+ arr[i]);

How do you declare an array of size 10 in Java?

Array Initialization in Java To use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets containing its size: int[] intArray = new int[10]; This allocates the memory for an array of size 10 .

Can we apply stream on array?

The stream(T[] array) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with its elements. It returns a sequential Stream with the elements of the array, passed as parameter, as its source.

How do you create an array in Java 8?

Using Arrays.copyOf() creates a new array by copying another array. The method has many overloads, which accept different types of arguments. Let's see a quick example: int array[] = { 1, 2, 3, 4, 5 }; int[] copy = Arrays.


1 Answers

When you start a literal integer with 0, it's considered an octal number, one that uses base 8 rather than base 10. That means 8 and 9 are not valid digits.

If you really want a leading zero (i.e., octal), you would need something like:

int[] monthValidDosInputs = {000, 001, ..., 007, 010, 011, 012, 013, 014};

which gives you the decimal numbers 0 through 12, but in octal.

However, if you want to keep them evenly spaced with decimal, just use a leading space instead of zero:

int[] monthValidDosInputs = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12};
//                           ^^  ^^  ^^  ^^  ^^  ^^  ^^  ^^  ^^  ^^  ^^  ^^  ^^

although I'm not sure you gain anything from that. You may as well just use:

int[] monthValidDosInputs = {0,1,2,3,4,5,6,7,8,9,10,11,12};

and be done with it. It makes no difference to the compiler.


If you're looking to use these to check user input (where they may enter 8 or 08 for a month), you're better off either:

  • using strings to check against; or
  • reducing their input to an integer (using something like Integer.parseInt(str,10)) so that there's no difference between 04 and 4.
like image 70
paxdiablo Avatar answered Oct 03 '22 11:10

paxdiablo