Home > Software engineering >  Java: Program says out of index but everything in code is correct
Java: Program says out of index but everything in code is correct

Time:11-05

A few days ago I had to finish my school homework and I came up with this simple program that returns true or false value depending on what the user enters in input. If word ends with character 'a' or 'e' program should return true value otherwise false.. We strictly needed to create our own method using loop and NOT USE endsWith or any other class. So my idea was basically to split the word into characters and fill a table with them and then at the end I used the if statement to check if the character saved in the table index matches 'a' or 'e'. I'm not interested in other solutions I just want an explanation of why the program always returns the error listed below.

P.S. I'm not an advanced programmer in any programming language so don't judge me.

Error:

java.lang.ArrayIndexOutOfBoundsException: 0
    at Homework.myMethod(Homework.java:34)
    at Homework.main(Homework.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)enter code here
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)  

Program:

import java.io.*;
public class Homework
{
  public static void main(String[] args) throws IOException
  {
    //Defined reader
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    //Instruction for user
    System.out.println("Please enter any word you like:");
    
    //Reading user input
    String a = in.readLine();
    
    //Showing user output
    if(myMethod(a) == true){
      System.out.println("Word has char 'a' or 'e' at the end of the word.");
    }
    else{
      System.out.println("Word doesn't have char 'a' or 'e' at the end of the word.");
    }
  }
  
  //My defined method
  public static boolean myMethod(String str)
  {
    //Empty table
    char table[]={};
    
    //Loop
    for(int i = 0; i < str.length(); i  )
    {
      //Grabs first char of word and adds it to the table
      table[i] = str.charAt(i);
    }
    
    //Check statement to see if there is actually char 'a' or 'e' at the end of the word.
    if(table[table.length-1] == 'a' || table[table.length-1] == 'e')
    {
      return true;
    }
    return false;
  }
}

CodePudding user response:

The problem is char table[] = {} that is an empty array, then you can't assign value in it, you may need to create it with a size

char[] table = new char[str.length()];

There is a better to do this which is str.toCharArray(). Also when you return true/false based on a condition, just return the condition

public static boolean myMethod(String str) {
    char[] table = str.toCharArray();
    return table[table.length - 1] == 'a' || table[table.length - 1] == 'e';
}

Also Scanner is a bit easier to use, and if (myMethod(a)) is enough as condition

Scanner in = new Scanner(System.in);
System.out.println("Please enter any word you like:");
String a = in.nextLine();
if (myMethod(a)) {
    System.out.println("Word has char 'a' or 'e' at the end of the word.");
} else {
    System.out.println("Word doesn't have char 'a' or 'e' at the end of the word.");
}

CodePudding user response:

Arrays are of fixed sizes. Your array is essentially of length 0. Try something like

char table[]= new char[str.length()];

CodePudding user response:

If you must use a for loop you can use this, which is essentially (now that I look) the same method as azro's except that the toCharArray() method is explicitly written out as a loop...

if (str == null || str.length() < 1) return false;

char[] chars = new char[str.length()];
for (int i = 0; i < chars.length; i  ) chars[i] = str.charAt(i);
return (chars[chars.length - 1] == 'a' || chars[chars.length - 1] == 'e');
  • Related