The following is a piece of code for performing linear search:

import java.io.*;

class search
{
      String str;
      int key,size,seaArr[];
	  
	  public void getdata()
	  {
	         System.out.print("Enter how many data you want to enter : ");
			 System.out.flush();
			 try{
			     BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			     str=obj.readLine();
				 size=Integer.parseInt(str);
				 seaArr=new int[size];
				 for(int i=0;i<size;i++)
				   {
				       System.out.print("Enter element at "+(i+1)+"th position  :  ");
					   System.out.flush();
					   str=obj.readLine();
					   seaArr[i]=Integer.parseInt(str);
				   }
			    }
			catch(Exception e)  {}
	  }
			     
	  public int LinSrch()
	  {
	        System.out.println("=====LINEAR SEARCH=====\n");
			getdata();
			System.out.print("\nEnter Search Key : ");
			System.out.flush();
			try{
			  BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
			  str=obj.readLine();
			  key=Integer.parseInt(str);
				for(int i=0;i<size;i++)
					{
					      if(seaArr[i]==key)
					           return(i+1);
					 }
				 }
			 catch(Exception e) {}
			 return(0);
	    }	
}

class  linSea
{
	public static void main(String args[]) 
	{
           search o1 = new search();
		   int result;
		   result=o1.LinSrch();
		   if(result==0)
		         System.out.println("\nSearch Not Found");
			else
			     System.out.println("\nSearch is Located at "+result+" Position");
	}
}

But whenever i execute the program I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: linSea

where linSea is the name of the file.
Please help me solve this query of mine.
Thanks

Recommended Answers

All 5 Replies

that one class was not needed:

import java.io.*;

public class search
{
      String str;
      int key,size,seaArr[];
	  
	  public void getdata()
	  {
	         System.out.print("Enter how many data you want to 

enter : ");
			 System.out.flush();
			 try{
			     BufferedReader obj=new 

BufferedReader(new InputStreamReader(System.in));
			     str=obj.readLine();
				 size=Integer.parseInt(str);
				 seaArr=new int[size];
				 for(int i=0;i<size;i++)
				   {
				       System.out.print("Enter 

element at "+(i+1)+"th position  :  ");
					   System.out.flush();
					   str=obj.readLine();
					   

seaArr[i]=Integer.parseInt(str);
				   }
			    }
			catch(Exception e)  {}
	  }
			     
	  public int LinSrch()
	  {
	        System.out.println("=====LINEAR SEARCH=====\n");
			getdata();
			System.out.print("\nEnter Search Key : ");
			System.out.flush();
			try{
			  BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
			  str=obj.readLine();
			  key=Integer.parseInt(str);
				for(int i=0;i<size;i++)
					{
					      if(seaArr[i]==key)
					           return(i+1);
					 }
				 }
			 catch(Exception e) {}
			 return(0);
	    }	
	public static void main(String args[]) 
	{
           search o1 = new search();
		   int result;
		   result=o1.LinSrch();
		   if(result==0)
		         System.out.println("\nSearch Not Found");
			else
			     System.out.println("\nSearch is Located 

at "+result+" Position");
	}
}

that one class was not needed:

import java.io.*;

public class search
{
      String str;
      int key,size,seaArr[];
	  
	  public void getdata()
	  {
	         System.out.print("Enter how many data you want to 

enter : ");
			 System.out.flush();
			 try{
			     BufferedReader obj=new 

BufferedReader(new InputStreamReader(System.in));
			     str=obj.readLine();
				 size=Integer.parseInt(str);
				 seaArr=new int[size];
				 for(int i=0;i<size;i++)
				   {
				       System.out.print("Enter 

element at "+(i+1)+"th position  :  ");
					   System.out.flush();
					   str=obj.readLine();
					   

seaArr[i]=Integer.parseInt(str);
				   }
			    }
			catch(Exception e)  {}
	  }
			     
	  public int LinSrch()
	  {
	        System.out.println("=====LINEAR SEARCH=====\n");
			getdata();
			System.out.print("\nEnter Search Key : ");
			System.out.flush();
			try{
			  BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
			  str=obj.readLine();
			  key=Integer.parseInt(str);
				for(int i=0;i<size;i++)
					{
					      if(seaArr[i]==key)
					           return(i+1);
					 }
				 }
			 catch(Exception e) {}
			 return(0);
	    }	
	public static void main(String args[]) 
	{
           search o1 = new search();
		   int result;
		   result=o1.LinSrch();
		   if(result==0)
		         System.out.println("\nSearch Not Found");
			else
			     System.out.println("\nSearch is Located 

at "+result+" Position");
	}
}

Thanks but the error is not yet resolved. this time i got the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: search

Ok, it worked for me, so this tells me it's your enviroment variables. So why don't you post your classpath and path variables, and I'll see what I can do..

You can usually put this in front of you classpath and it wili work:

.;%classpath%.;.;

Also make sure your classpath variable is pointing to tools.jar

Check your class definition.

Your main class MUST be public (though some runtime versions (especially older ones) may have permitted default access that's no longer the case and was never recommended).

Ok, it worked for me, so this tells me it's your enviroment variables. So why don't you post your classpath and path variables, and I'll see what I can do..

You can usually put this in front of you classpath and it wili work:

.;%classpath%.;.;

Also make sure your classpath variable is pointing to tools.jar

Thanks a tonne, but I realized where I was wrong. Actually I had specified the wrong path in the autoexec.bat and it had to be updated. That's it. Thanks anyway to both of you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.