473,465 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help Writing a certain method

4 New Member
I'll try to explain this the best I can.

I have a class called book which creates books that each hold information such as author, title, ISBN code, etc.

Then I have another class called booklist which stores the books in an array list.

In the booklist class I am trying to write a method which displays the books only written by a specific author.

In my book class I have these methods that will help with the method I am creating:

public void addAuthor(String f, String l)
{
author = f + " " + l;
}

public String getAuthor()
{
return author;
}

public void display()
{

System.out.println("Title:\t" + title);
System.out.println("Author:\t" + author);
System.out.println("Year Published:\t" + year);
System.out.println("ISBN Code:\t" + isbn);
System.out.println("Status:\t" + status);

}

This is the method I am trying to create:

public void showBooks (String first, String last)
{
for(Book book : list)
{
if(book.getAuthor() == first + " " + last)
{
book.display();
}
}
}

I can't figure out why showBooks isn't working. It's not displaying anything even after executing these statements:

b1.addAuthor ("Ian","Fleming");
myBooks.showBooks("Ian","Fleming");

Any suggestions?
Mar 5 '07 #1
1 1215
r035198x
13,262 MVP
I'll try to explain this the best I can.

I have a class called book which creates books that each hold information such as author, title, ISBN code, etc.

Then I have another class called booklist which stores the books in an array list.

In the booklist class I am trying to write a method which displays the books only written by a specific author.

In my book class I have these methods that will help with the method I am creating:

public void addAuthor(String f, String l)
{
author = f + " " + l;
}

public String getAuthor()
{
return author;
}

public void display()
{

System.out.println("Title:\t" + title);
System.out.println("Author:\t" + author);
System.out.println("Year Published:\t" + year);
System.out.println("ISBN Code:\t" + isbn);
System.out.println("Status:\t" + status);

}

This is the method I am trying to create:

public void showBooks (String first, String last)
{
for(Book book : list)
{
if(book.getAuthor() == first + " " + last)
{
book.display();
}
}
}

I can't figure out why showBooks isn't working. It's not displaying anything even after executing these statements:

b1.addAuthor ("Ian","Fleming");
myBooks.showBooks("Ian","Fleming");

Any suggestions?
When comparing two strings in java, do not use if(string1 == string2). The == checks if the two refer to the same position in memory. Use the .equals method which compares the equality of the contents of the string variables.
Mar 5 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
25
by: David Bernier | last post by:
I'd like to pass on the command line two filenames. As for example: my_executable filename_1 filename_2 I haven't done any C programming with command line arguments so far. I'm familiar...
27
by: SK | last post by:
Hi I am trying to teach myself how to program in C. I am a physician hoping to be able to help restructure my office. Anyhow, I amhoping that the porblem I am having is simple to those much more...
3
by: Jesper Denmark | last post by:
Within the following construction switch (expression) { int i; i = GetArgs() //return 2 case constant-expression:
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
6
by: bonk | last post by:
I am trying to create a stream that writes text to a file and: - automatically creates a new file once the current file exceeds a certain size - makes it possible to be used by multiple threads...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: meripu1 | last post by:
Im almost certain that I will be unable to provide enoguh information for anyone to help me with this, but I though I would try anyway. I am writing some code for a bank and have written a method so...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.