0

I want 31842 to be stored in an array:

arr[49] would store 2

arr[48] would store 4

arr[47] would be 8

arr[46] would be 1

arr[45] would be 3

arr[0]..arr[44] would all be 0

I wrote code to do it, but an ArrayIndexOutOfBoundsException error bumps up! Any ideas?

  public static void main(String[] args)
  {
    Scanner scan = new Scanner (System.in);
    int [] integer1 = new int[50];
    int [] integer2 = new int[50];
    String string;
    char ch;

    System.out.print("Please enter an integer #1: ");
    string = scan.nextLine();
    for (int i = integer1.length; i > 0; i--){
      int position = string.length()-1;
      ch = string.charAt(position--);
      if (ch >= '0' && ch <= '9'){
        int chToInt = ch - '0';
        integer1[i] = chToInt;
        System.out.println(integer1[0]);
        }
      else{
        int chToInt = 0;
      }
    }
 }
Eran
  • 387,369
  • 54
  • 702
  • 768
BAS
  • 145
  • 1
  • 11

4 Answers4

3

Change:

for (int i = integer1.length; i > 0; i--){

to:

for (int i = integer1.length - 1; i >= 0; i--){

integer1.length is 50, and it's not a valid index in your array. Array indices start at 0 and end at array.length - 1.

That's not the only problem with your code, though:

position should be initialized outside the loop, and ch = string.charAt(position--); is valid only as long as position >= 0.

This should work:

int position = string.length()-1;
for (int i = integer1.length - 1; i >= 0; i--){
  if (position >= 0)
    ch = string.charAt(position--);
  else
    ch = 0;
  if (ch >= '0' && ch <= '9') { 
    integer1[i] = ch - '0';
    System.out.println(integer1[i]);
  } else {
    integer1[i] = 0;
  }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
  • 4
    You should change to `i >= 0` too. – BackSlash Nov 26 '14 at 08:39
  • 1
    @BackSlash That's correct, though it would make no difference in this specific case, since OP wants the start of the array to holds zeroes, which is the default value. – Eran Nov 26 '14 at 08:42
  • @Eran thank you! It worked perfectly.. It always seems silly after discovering the issue!! :) – BAS Nov 26 '14 at 10:00
-1

int i = integer1.length -1

Change to this in the for loop

and also intitialize the position variable outside of the for loop, else it will always give you the same character

 public static void main(String[] args){
    Scanner scan = new Scanner (System.in);
    int [] integer1 = new int[50];
    int [] integer2 = new int[50];
    String string;
    char ch;

    System.out.print("Please enter an integer #1: ");
    string = scan.nextLine();
    int position = string.length()-1;
    for (int i = integer1.length - 1; i >= 0; i--){

      ch = string.charAt(position--);
      int chToInt;
      if (ch >= '0' && ch <= '9'){
        chToInt = ch - '0';
        System.out.println(integer1[0]);
        }
      else{
        chToInt = 0;
      }
      integer1[i] = chToInt;
    }
 }
Eran
  • 387,369
  • 54
  • 702
  • 768
sanjay Kumar
  • 140
  • 1
  • 14
-1

Arrays are zero-based in Java, if you have an array of size 50, the indexes will run from 0 to 49:

0 1 2 3 4 ... 49 - sums up to 50.

See the JLS - Chapter 10. Arrays for details:

... we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.

So when you do:

for (int i = integer1.length; i > 0; i--)
                     ↑
             length returns 50

You're actually out of bounds. Change integer1.length to integer1.length - 1.

Also note that you're not taking the very first element, which is at the 0 position, you'll also need to change i > 0 to i >= 0.

Maroun
  • 94,125
  • 30
  • 188
  • 241
-1

Array length and array last index are not same. Last index of an array always array length-1 since index of an array starts with 0 rather than 1.

so, change your for loop

for (int i = (integer1.length - 1); i >= 0; i--){

//do operations
}
Sagar Pudi
  • 4,634
  • 3
  • 32
  • 51