• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does this look right (about arrays)?  RSS feed

 
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing a problem where I get/set methods for a book title, author, and pages. I then create an application where the user picks a book. I was having a
hard time with the following part because I kept getting an error at: System.out.println(catalog[x].getbookTitle() + " "); The error would be, "catalog cannot be resolved to a variable."

I tried adding the array declaration outside the main method but it still gave an error. I'm using Eclipse and it recommended I make it static. With that, does
this look correct? I'm only asking because the last "catalog" is now in italics and I didn't know if that will mean something down the line.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a little difficult to work out what exactly you are trying to do, but I see the following problems.

1. You have two catalog variables - one declared on line 3 and the other on line 8.

2. You only initialise the one declared on line 8.

3. Having initialised it, you then overwrite all the book titles with " ". Lines 21 - 23.

4. You then print out the book titles followed by a space, which means you will print fives lines each with two spaces on it.

5. You pass a String array to the display method and you iterate over this array but print out the book titles from the LibraryBook array you declared on line 3. This would cause a ArrayIndexOutOfBoundsException if the bookTitle array was bigger than 5 elements.

6. However, you won't get an ArrayIndexOutOfBoundsException because the catalog array (the one decalred on line 3) is empty (because you never add anything to it), so, if you do call the display method, you will get a NullPointerException when you try to call the getbookTitle method.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need to do (I think) is

1. Make the catalog variable on line 3 non static.

2. Create a constructor for your class that takes a LibraryBook array as a parameter. In the constructor, assign the parameter to the catalog variable.

3. Get rid of lines 21 - 27.

4. Instead, create an instance of the LibraryBookSort class passing the catalog variable created on line 8 to the constructor.

5. Get rid of the bookTitle parameter of the display method.

6. Make the display method non static.

7. Call the display method using the class instance you created in step 4.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses. I got even more confused after posting the question and went with this (which seems to work). I'm supposed to ask the user
what order they want the information (like author, title, pages or title, pages, author, etc.) and display it that way. Just sort of staring at it in confusion now.

Mmmm...I feel like I get the gist of what I'm learning at the time but then have a really hard time pulling it all together.



This is my class:

 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy

I see you got your first part solved, now the sorting right? From the code you have, I reckon the output will be the same as your insertion order or array index to be exact.

Since the LibraryBook object has 3 attributes, and I assume it is possible to sort by all 3 ... but start with title. How to sort by title? Check out the Arrays.sort() method.

Further to sorting by title, you may want to sort by author or pages. Then check out java.util.Comparator. Even title can use the comparator.

I hope this isn't too much to take in. Happy coding.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much! I'll try it when I get home.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I came up with and it works but I'm curious if it's considered sloppy or inefficient programming. I'm guessing it is since it seems really long winded if
there were more fields than author, title, and pages to consider. It just seems like a lot of code for something pretty trivial.

Also, while I did it, I don't really understand what this part of the code actually means (how it goes from if to switch):



This is the full code:



Thanks for any advice.

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote: . . .

. . .
Aaaaaaaaaaaaaaaaaaaaaaaargh!

There is something people call the DRY principle; it stands for Don't Repeat Yourself. You have written the same code 6 times on the same page. You need to find out how to iterate an array. Or how to get the nth element of an array, remembering its first element is numbered 0. I hope you will find it in the Java Tutorials here or here. And using your loop to get at the values of the Book class is non‑object‑oriented style. you should have a toString method in the book class. Then you can simply write
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
There is something people call the DRY principle; it stands for Don't Repeat Yourself. You have written the same code 6 times on the same page. You need to find out how to iterate an array. Or how to get the nth element of an array, remembering its first element is numbered 0. I hope you will find it in the Java Tutorials here or here. And using your loop to get at the values of the Book class is non‑object‑oriented style. You should have a toString method in the book class. Then you can simply write


lol...sorry, sorry. I will look and try my best.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to get that block of code I quoted down to three lines.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at those pages and am guessing it would be something with the format of below but I have no idea how I'd code and have it print out in such few lines.

 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:I looked at those pages and am guessing it would be something with the format of below but I have no idea how I'd code and have it print out in such few lines.



Take a look at the switch statement you wrote. Do you notice that the contents of each case are identical? The switch statement isn't actually doing anything for you there. Have a think about what it is you're trying to achieve. Draw the data structure you have on a piece of paper, then try and think how the for loop can help you achieve what it is you're trying to do.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've looked at a bunch of websites but just don't understand the syntax for what's above or similar problems. I understand what I'm looking at with examples
but can't find any so far that help me understand the above. I'll keep looking since I seem to be doing the same thing on a lot of problems. Thanks for the tips.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get yourself a periodic table of the elements. Remember computers work on Si for Silicon. Find silicon, and go one up. There you find C for carbon. You are going to have to abandon silicon‑based technology for a bit. Uncouple the Si from yourself by turning it off. Then you won't be able to read the rest of this post.
Get a pencil (carbon up the middle: the more carbon (i.e. soft pencil) the better. Get some paper (contains carbon) and an eraser (usually based on carbon polymers ‍). You are going to find the eraser very useful.
Now write down simply in English what you are going to do. If you write anything that looks even slightly like Java® or any other computer language, erase it and start again. Do you want to print every element of the array, a particular element, one chosen by the user by entering a number, or what? When you have that written so simply and clearly that even I could understand it, then you might consider changing it to code.

Remember what I said about three lines earlier. You are looking for something simple. Very simple. And you have been given hints to most of the answer already in this thread.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this is correct but I've done this and it will print out all the selections. I've been messing around and searching online
but can't figure out how to only print what the user selected. (I made it choice+1 because it would give me an outofbounds exception if I put "6".)

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to print out books[i]. Go back to my earlier posts; I told you what to do.
There is something wrong with the for loop. If you are iterating an array, see whether you can use a for‑each loop (officially called enhanced for). If you can't use an enhcnaed for, always write the loop like thisKeep the spacing, position of brackets, etc, exactly as I showed you there. Note that I indented by 4 spaces. Not tabs.
That form of a for loop can always be relied on never to throw Exceptions because you have overrun the bounds of the array. Your loop risks going beyond the bounds of the array. I am not sure whether you want to print all the books or one; your code looks as if you are trying to do half‑and‑half. The for loop also looks as if you have been guessing rather than working it out. You can guess a million times and chances are one of the guesses will work. Or you can think about it and the first attempt will work.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I don't know. Thanks for the help.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I told you what to do in a previous post. You even quoted what I wrote in one of your posts. Go back and read it again, carefully. I said, “You should have a … ”
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should be able to get that block of code I quoted down to three lines.


Sooo...it's been five months and, while I've learned other things, I still don't know how to get that down to three lines. Guess I'm still just really bad at iterating an array.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have quite forgotten. Sorry.

Actually I may have missed that you wanted the details in different orders. That is the sort of thing that should be done inside the book class.Make Order an enum inside that Book class and you can add methods to its elements to produce the output.
You can choose enum elements by their name or their ordinal (0...5 in this case). Read about enums in the Java Tutorials. There is probably some way you can shorten those methods with a λ expression.

I have no idea whether that lot will work, but try it and see what happens.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell. I meant more I'm a moron for not figuring it out after this much time. I never heard of enum but will read up on it today.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An enum is a really useful thing. You can create one with exactly six elements and those six elements will remain for the entire lifetime of the JVM.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!