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 asStart 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..