Page 2 - Discuss Adding Books to a Library Object in the Java Help forum on Dev Shed. Adding Books to a Library Object Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 36
Time spent in forums: 7 h 30 m 26 sec
Reputation Power: 1
One thing that has always helped me when writing larger programs is to test certain aspects of my program as they are written. For example, if I were writing your Book program, I would write a class for file input. Then I would test it and ensure that it was performing the input properly.
So, my advice is to ensure that one part of your program is working, then move onto the next. If you are having issues getting a specific piece working, then put it up here with input and output and describe the problem.
Posts: 30
Time spent in forums: 8 h 5 m 54 sec
Reputation Power: 1
Quote:
Originally Posted by Cameron0960
One thing that has always helped me when writing larger programs is to test certain aspects of my program as they are written. For example, if I were writing your Book program, I would write a class for file input. Then I would test it and ensure that it was performing the input properly.
So, my advice is to ensure that one part of your program is working, then move onto the next. If you are having issues getting a specific piece working, then put it up here with input and output and describe the problem.
Thanks for your response, i totally agree.
My program is running fine up to this point, works great, and reads in values/user input as it should.
Unfortunately i don't know how to make the program print out the data that has been input.
ie. After inputing 3 Books worth of data, i want the program to then print the smallLib, in turn, printing out the books details that were intially put in.
I have ok knowledge with smaller programs, arraylists, switches, loops, printlns etc. But for some reason, i have no idea how to take this further java wise.
In my head i know exactly what i want ha, its just writing it...
Thanks again anyway.
Posts: 36
Time spent in forums: 7 h 30 m 26 sec
Reputation Power: 1
Quote:
Originally Posted by qwerty101
Thanks for your response, i totally agree.
My program is running fine up to this point, works great, and reads in values/user input as it should.
Unfortunately i don't know how to make the program print out the data that has been input.
ie. After inputing 3 Books worth of data, i want the program to then print the smallLib, in turn, printing out the books details that were intially put in.
I have ok knowledge with smaller programs, arraylists, switches, loops, printlns etc. But for some reason, i have no idea how to take this further java wise.
In my head i know exactly what i want ha, its just writing it...
Thanks again anyway.
What I normally do for output is write a method for each of my classes that look like the following:
Code:
Class Book {
private String title;
public Book (String title) {
this.title = title;
}
public String toString() {
String output = "";
output += "Title: " + title;
return output;
} // closes toString() method
After writing such a method, I am able to simply add the following code to my main to print the contents of that object:
Code:
public static void main(String[] args) {
Book book1 = new Book("Oliver Twist");
System.out.println(book1); // this will call the toString() method of the book class
So, the output of this would be "Oliver Twist".
In your example, you are storing Book objects in an ArrayList. So, what I would recommend is writing a toString() method in the class you've written that contains the arrayList data structure. Have that method go through each index of your arrayList, get the Book object at position index, concatenate all the output into a single String and return it.
Then, you will be able to simply say in your main:
Code:
public static void main() {
BookList books = new BookList();
System.out.println(books);
This will then output all the books that are stored in the books instance of BookList.
Hope that helps, I can show you an example of printing the entire contents of an arrayList using the method I mentioned above if it would help further. Although, if I did that, I would basically be handing you code lol..