0

I'm learning java now and currently working on this problem:

Save the string “WELCOMETOZOHOCORPORATION” in a two dimensional array and search for substring like “too” in the two dimensional string both from left to right and from top to bottom.

w e L C O M E T O Z O H O C O R P O R A T I O n
And print the start and ending index as

Start index : <1,2>

End index: <3, 2>

and here's my code for it;

public class WelcomeToZoho {
public static void main(String[] args){
    String str = "WELCOMETOZOHOCOORPORATION ";
    String[] strSplitArray = str.split("");
    String[][] strArray = new String[5][5];
        int k=0;
        for(int i=0;i<5;i++) {
            for(int j=0;j<5;j++) {
                strArray[i][j] = strSplitArray[k];
                k++;
            }
        }   
        try {for(int i=0;i<5;i++) {
            for(int j=0;j<5;j++) {

            if(strArray[i][j].equals("T")&&strArray[i][j+1].equals("O")&&strArray[i][j+2].equals("O")) {
                System.out.println("Start Index <"+i+","+j+">");
                System.out.println("End Index <"+i+","+j+2+">");
            }
            else if(strArray[i][j].equals("T")&&strArray[i+1][j].equals("O")&&strArray[i+2][j].equals("O")) {
                System.out.println("Start Index <"+i+","+j+">");
                i=i+2;
                System.out.println("End Index <"+i+","+j+">");
            }
            }           
            }
        }catch(Exception e){
            System.out.println("array out of bound occurs");
        }       
  }
}

I know this exception is caused by the second 'T' in the array where i+1 returns the exception, so i tried to use the try and catch method. Without using the try catch, i'm getting the required output along with the exception but using try catch, i'm not able to get the output... any clue how to get the output but not the exception together?? i'm a beginner, so please bear with my code..

  • You shoudn't catch that exception. Remove the try cacth completely, and instead **avoid** the exception by fixing your code and avoiding to access invalid indices. `strArray[i][j+1]`, for example, tries to access index j+1. When value of j is 4, j+1 is thus 5, which is an invalid index since your array only has 5 elements (and indices thus go from 0 to 4). – JB Nizet Oct 18 '17 at 05:41
  • change your looping to `for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { ` – Scary Wombat Oct 18 '17 at 05:43
  • ScaryWombat changing the loop to 3 didn't work.. @JBNizet, without the try-catch, i'm able to get the output along with the exception and i understand why it's coming.. I don't know how to fix the code like tell it not to look for the index j+1 i.e,5 when j is 4 – Shravan Oct 18 '17 at 05:55
  • In that case your logic is wrong - step through your code with a debugger and find the error – Scary Wombat Oct 18 '17 at 05:57
  • @ScaryWombat there is no error in the code.. there is only the array out of bound exception caused because of exactly what JB Nizet said. when the compiler finds the second T in (welcome"T"ozohocorpora"T"ion) it's looking for i+1 which is out of the array, hence the exception occurs – Shravan Oct 18 '17 at 05:59
  • @Shravan I won't write the code for you (I don't know what it's supposed to do anyway). But Java has `if` statements. So you can test the value of i` and `j` before accessing an index that would be invalid. loops have bounds, too, so you can fix the bounds of loops to avoid accessing invalid indices. Accessing invalid indices causing such an exception IS is huge error in the code. Your code has bugs that need to be fixed, not worked around. – JB Nizet Oct 18 '17 at 06:00
  • *there is no error in the code* **then it should work shouldn't it** – Scary Wombat Oct 18 '17 at 06:01
  • okay JBNizet will check them out.. thank you :) and @Scary Wombat, I'm getting the output successfully, but i'm getting the exception along with the output, and i know why that exception is occuring, but just couldn't sort it out... EDIT: i got it sorted by adding these lines, if(i==4||j==4) { break; } thank you guys :) – Shravan Oct 18 '17 at 06:05
  • If you get the correct result then return immediately, but it will still fail **if** you can not get a match – Scary Wombat Oct 18 '17 at 06:06
  • @ScaryWombat i got it sorted by adding these lines, if(i==4||j==4) { break; } thank you :) yeah return worked too! – Shravan Oct 18 '17 at 06:07

0 Answers0