I wrote this simple piece of code to find the number in the given array of Integers.The program works fine although it shows a couple of errors when i modify it a little bit.public class DemoLabel{ public static void main(String [] args){ int[][] arrofInts={ {32,45,67,87}, {23,44,55,66}, {12,47,87,56}, {23,44,12,78} }; int searchFor=12; int i; int j=0; boolean foundIt=false; search: System.out.println("HELLO");//Shows error for(i=0;i<arrofInts.length;i++){ for(j=0;j<arrofInts[i].length;j++){ if(arrofInts[i][j]==searchFor){ foundIt=true; disp(i,j,searchFor); continue search; } } } /*if(foundIt){ System.out.println("The number " + searchFor+" was found at " + i+","+j); } else{ System.out.println("The number was not found "); }*/ } public static void disp(int i, int j,int searchFor){ System.out.println("The number "+searchFor+" is at the location "+i+" , "+j); } }
In the disp() method,Suppose i only want to pass two arguments i.e disp(i,j) given that searchFor variable is visible in the entire class but when i use disp(i,j) i get the following error DemoLabel.java:38: error: cannot find symbol System.out.println("The number "+searchFor+" is at the location"+i+" , "+j);
^
symbol: variable searchFor
location: class DemoLabel
So i decided to declare theasint searchForbut the compiler threw another error saying that it was a Illegal start of expressionpublic int searchFor
Also,when i add a SOP line after the label search: it shows the error DemoLabel.java:25: error: undefined label: search
continue search;
^
Although i got the program to work it would be helpful if i could understand why it doesn't work with the modifications.
Thanks.