Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Simple help in Java !!

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Simple help in Java !!

    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 the
    int searchFor
    as
     public int searchFor
    but the compiler threw another error saying that it was a Illegal start of expression

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,394
    Thanks
    49
    Thanked 2,212 Times in 2,185 Posts

    Default Re: Simple help in Java !!

    Illegal start of expression
    Variables in methods can't be accessed from outside of the method. public is not valid there.
    error: undefined label: search
    I think labels MUST be on loop statements like for and while. Not just close to them
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    thebenman (May 2nd, 2014)

Similar Threads

  1. Simple java program help please.
    By Arg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 28th, 2014, 03:07 AM
  2. Simple Java compiler
    By cazanova in forum Java Theory & Questions
    Replies: 2
    Last Post: May 29th, 2013, 02:09 AM
  3. Need some simple Java Help
    By traewall11 in forum Object Oriented Programming
    Replies: 11
    Last Post: September 26th, 2012, 05:11 PM
  4. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM
  5. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM