• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

length of array

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ,

I want to ask about the length of the array.

//create new object
Book[] BookList = new Book[MAX_SIZE];

//constructing 5 book object
BookList[0]= new Book("Absolute Java","Savitch",5,true);
BookList[1] = new Book("JAVA: How to Program","Deitel and Deitel",0,true);
BookList[2] = new Book("Computing Concepts with JAVA 3 Essentials","Horstman",5,false);
BookList[3] = new Book("Java Software Solution","Lewis and Loftus",5,false);
BookList[4] = new Book("Java Program Design","Cohoon and davidson",1,true);

why I can't use BookList.length?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anne,
You can. It gives you the size of the array including any nulls present. If you don't want to count nulls, you'll need to loop through the list.

Also note is is convention to have lowercase variables names in Java so BookList would be bookList.
 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for reply.
if I want to use BookList.length here:



why I've got the error BookList
length can't be resolve or it's not a field.?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anne Saizan wrote:


You can do that. If it's not working, I suspect something else is going on that we can't tell from the code you've posted. Can you post a (simple) full example that demonstrates the problem?
 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is my code:

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that might be the problem. You've got a class called BookList, so the compiler is getting confused between the class and variable with the same name.

You'd avoid the problem if you followed Jeanne's suggestion and followed the standard Java naming conventions. Classes should start with a capital, and variables should start with lower case. Then the compiler won't get confused between bookList and BookList.
 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay thanks. If I delete the BookList class, and alter the variables, the error is still the same.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, after doing that, the next error is that the bookList variable is out of scope in the searchBook method. It needs to be passed into the method, or declared somewhere that puts it in scope.

(There are also a couple of missing }s in that code, so it won't compile anyway, but that's easily fixed).
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you haven't given the entire code......for example you are importing the Scanner class but no input from user is taken, secondly there are methods which you have defined and never called??>???
 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Matthew it's working now.But there's an error when I tried to pass the parameter to searchBook() method.


Thanks Rameshwar for the responds. But the code is not completed yet. I'm still working on it.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
searchBook() method takes 1 parameter and at the time of invoking the method you are passing 2 parameters.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anne Saizan wrote:[code=java]
index = searchBook(input,listBook[]); //error: listBook[] can't be resolved to variable.


listBook[] isn't a variable name. Just try listBook.
 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
owh sorry for that. This is the latest code:

I've got problem to pass two parameters in searchBook() method when I want to invoke that method here:




 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew, when I change to listBook.
The same error occurred.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's difficult to comment when I don't know what the current state of the code is. But if you've got a method:
and you've want to call it, the syntax will be something like:

Note I use Book[] listBook instead of Book listBook[]. Both are valid, but I think my version is clearer. It's a variable called listBook of type "array of Book". The other syntax suggests that the [] is part of the variable name, leading to the sort of errors your code above has.
 
Anne Saizan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I understand that. Thank you for helping =)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic