user2078583
user2078583

Reputation: 1

.class expected error for my program

cant compile this prog 4 searching a number in a array.. .class expected error at last line of prog : obj.searchnumber(int arr1[],item);

import java.util.*; 
    public class Ans5
    {
      public void searchnumber(int arr[],int item){
          int low = 0;
          int high = arr.length-1;
          int mid;
          while (low <= high) {
            mid = (low + high) / 2;
            if (arr[mid] > item)               high = mid - 1;
            else if (arr[mid] < item)          low = mid + 1;
            else                            
                System.out.println("The searched item"+item+"is found in the array");
          }
      }

      public static void main(String args[]) {
          Ans5 obj= new Ans5();
          Scanner sc=new Scanner(System.in);
          int arr1[]={1,2,3,4,5,6,7,8,9};
          System.out.print("\fEnter the item u need to search: ");
          int item=3;//sc.next();
          obj.searchnumber(int arr1[],item); // here's the error shown at arr1[]
      }
    }

Upvotes: 0

Views: 106

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213243

You don't pass array like that in a method invocation. You just need to use the name of the array like this:

 obj.searchnumber(arr1, item);

int arr1[] form is just used at the time of declaration of the array. It simply creates a new array reference, and says that arr1 is of type int []. You can't use it in some method invocation or so. It's the arr1 which references the actual array. And you access the array through this name only.

Upvotes: 2

Arpit
Arpit

Reputation: 12797

obj.searchnumber(int arr1[],item);
                  ^^^^^^^^^________________Can't declare a variable here. if ypu want to pass the array then simply name it as @Rohit said

Upvotes: 1

Related Questions