2 Replies - 580 Views - Last Post: 22 March 2010 - 08:41 PM Rate Topic: -----

#1 AmateurC  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Compiler unable to find length of integer array

Posted 22 March 2010 - 08:27 PM

I am trying to write a program that searches an integer array. However, the compiler throws an error saying that it cannot find the method length(). What is my problem?
import java.util.*;
class linearSearchTester {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int[] array = {45,34,2,5,7,-333,7,8,0,-33,-75435566,675};  
    System.out.println("Enter integer: ");  
    int number = scan.nextInt();
    Search search = new Search();
    int result = search.linearSearch(array, number);  
  }
}

class Search {
   int linearSearch(int[] a, int key) {
     for (int i = 0; i < a.length(); i++) {
        if (key == a[i]) {
            return i; 
        }
    }
    return -1;        
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Compiler unable to find length of integer array

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Compiler unable to find length of integer array

Posted 22 March 2010 - 08:38 PM

Change

for (int i = 0; i < a.length(); i++) {

to

for (int i = 0; i < a.length; i++) { // no parens
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Compiler unable to find length of integer array

Posted 22 March 2010 - 08:41 PM

Yep! For future reference:
Arrays:
.length

Strings:
.length()

ArrayLists/Vectors:
.size()
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1