0

How to clear ArrayIndexOutOfBound exception in java

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at word.main(word.java:17)

My code as follows:

        import java.util.*;
        import java.lang.*;
        class word{

    public static void main(String[] args){
        String n;
        String a[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
        String b[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
        System.out.println("Enter number in words");
        Scanner sc=new Scanner(System.in);
        n=sc.next();
        for(int i=0;i<=20;i++){
            if(n==a[i]){
                System.out.println(i);
                break;
            }
            else if(n==b[i]){

                System.out.println(i);
                break;
            }
        }
    }
}
Abhishek
  • 281
  • 4
  • 16

3 Answers3

2

Honestly the easiest way to proceed here would be to either just use a single array for all the numerical words, or to use a more flexible data structure, such as a list. If you want to keep your current inline array definitions, but use a list, here is one option:

String[] a = { "zero", "one", "two", "three", "four", "five", "six", "seven",
               "eight", "nine", "ten" };
String[] b = { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
              "seventeen", "eighteen", "nineteen", "twenty" };
List<String> nums = new ArrayList<>();
nums.addAll(Arrays.asList(a));
nums.addAll(Arrays.asList(b));

System.out.println("Enter number in words");
Scanner sc = new Scanner(System.in);
String input = sc.next();

for (int i=0; i < nums.size(); i++) {
    if (input.equals(nums.get(i))) {
        System.out.println(i);
        break;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

Accessing an array with an index that is greater than or equal to the length of the array causes an 'ArrayIndexOutOfBoundsException'.

In your code, you're trying to access the 11th (and 12th, etc...) index a 10 length arrays. (A is 11, B is 10).

Considering you're apparently trying to output numerical value for a given "in letters" number, the best solution seems to be looping 21 times on a single array containing the 21 elements.

Last thing, you should always use array.length in a loop, using a litteral value like "20" means you'll have to edit it as soon as you edit your array.

To sum up, it would look like this :

  public static void main(String[] args){
        String n;
        String a[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"}; 
        System.out.println("Enter number in words");
        Scanner sc=new Scanner(System.in);
        n=sc.next();
        for(int i=0;i<a.length;i++){
            if(n.equals(a[i])){
                System.out.println(i);
                break;
            }
        }
    }
Alexandre Beaudet
  • 2,774
  • 2
  • 20
  • 29
  • @TimBiegeleisen Woops, C# dev here, forgot that java "==" is a reference test – Alexandre Beaudet Aug 03 '18 at 05:32
  • 1
    People who ask this in Java questions here often get blasted, but coming from a C# background is a legitimate reason to make this mistake IMO. – Tim Biegeleisen Aug 03 '18 at 05:33
  • still giving the same outofbound exception – Abhishek Aug 03 '18 at 05:35
  • Also, there is no need to loop over the array. Just catch the input number, and print the index string – Rcordoval Aug 03 '18 at 05:35
  • @Rcordoval I do agree that the solution isn't the prettiest, just wanted to stay as close as I could to his code so that he could understand why the exception was thrown – Alexandre Beaudet Aug 03 '18 at 05:36
  • Thanx that worked i was using== instead of .equals that's why it was making exception i think – Abhishek Aug 03 '18 at 05:40
  • @john ArrayIndexOutOfBound was caused by the code trying to access the 11th cell of the "b" array, == was just using string references instead of string values to do the test. Technically that would not throw an exception, but probably not the expected behaviorhttps://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java – Alexandre Beaudet Aug 03 '18 at 05:42
  • @john you will get that exception every time your input is not in the arrays, you have to take care of border cases. – Rcordoval Aug 03 '18 at 05:44
1

I will assume that is necessary that you keep the two arrays separetly. As others mentioned, you are trying to access a position in the array that exceeds the legnth of it. You already have pretty good solutions, but if you want to keep your code as it is you can try this.

public static void main(String[] args){
        String n;
        String a[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
        String b[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
        System.out.println("Enter number in words");
        Scanner sc=new Scanner(System.in);
        n=sc.next();
        for(int i=0;i<=20;i++){
            if(n.equals(a[i%a.length])){
                System.out.println(i);
                break;
            }
            else if(n.equals(b[i%b.length])){

                System.out.println(i);
                break;
            }
        }
    }
Cthulhu
  • 699
  • 1
  • 10
  • 21