2

This Java program does a simple computation and it is suppose to output the numerical value of 123+1

(The output should be 124.) (Ignore the last "+" string.)

I got an error inside the if statement:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4

I did a printout for arrayz[i-1] and arrayz[i+1] and it seems to printout 123 and 1 receptively, which is correct. So, I'm not sure what's wrong.

        String math = "123+1+";
        String arrayz[]={"123","+","1","+"};
        double total =0.0;

        int i=0;
        while(i<=(math.length()-1))        //don't bother with the last char
        {       i++;
            if(arrayz[i].equals("+"))
            {
                total = Double.parseDouble((String)arrayz[i-1]) + Double.parseDouble((String)arrayz[i+1]);

            }

        }

        System.out.println(total);
1
  • Increment i at the end of the loop, not the beginning. Commented Dec 28, 2013 at 6:13

3 Answers 3

3
   while(i<=(math.length()-1))  

math length is 6 and and in side loop your array length is 4

You might want to write

 while(i<=(arrayz.length-1))  
Sign up to request clarification or add additional context in comments.

Comments

1

Since you are using the index i with the array arrayz, you must use arrayz.length instead of math.length()

Edit

This should work:

public static void main(String[] args)
{
    String math = "123+1+";
    String arrayz[] = { "123", "+", "1", "+" };
    double total = 0.0;

    int i = 0;

    // arrayz.length - 2 -> because you are accessing "arrayz[i + 1]", 
    // arrayz.length - 1 would be OK if the maximum index you were using were "arrayz[i] "
    while (i <= (arrayz.length - 2)) //don't bother with the last char
    {           
        if (arrayz[i].equals("+")) {
            total = Double.parseDouble((String) arrayz[i - 1]) + Double.parseDouble((String) arrayz[i + 1]);
        }
        i++; // Increment at the end of the loop
    }

    System.out.println(total);
}

6 Comments

Try using length without ()
No way! What's the error? You may increment (i++) after all the computations, in other words, at the end of the loop. You could use a for loop to avoid this.
it works when I do while(i<=(arrayz.length-3)) but it doesn't look efficient.
What happen is that you are accessing with the index i+1, you must correct this too.
yup, I put the i++ after the if statement. I'm still getting the ArrayIndexOutOfBoundsException error
|
1

You are looping over the string math, at the same time accessing elements of arrayz inside your loop, and thinking that they have the same elements and the same length.
What I suggest you, is to use instead of math String (you can omit it in this case but I assume you can't in general for some criteria), you can use an array of type String, so that 123 in your example would be the first element arrayz[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.