help please - urgent exam question
I'm having difficulty with the following question for an OU exam:
You will now create a method to enable students to make a list of the books they have read, whether they are on the courses reading list or not.
Add an instance method to the class, with the header: public Set<String> collectBooksRead()
The method should first declare a local variable of type Set called readBooks capable of holding a set of strings.
The method should then assign to that variable an appropriate set object.
The method should then use a dialogue box to collect the book titles one at a time from the user.
The dialogue box should have the prompt "Please input a book title or * to finish", and should have an empty string as its default reply.
Each entered book title should be added to the set referenced by the local variable readBooks. Finally the method should return readBooks as the message answer.
Here's my code so far but it endlessly loops and I'm unsure how to make it stop.
public Set<String> collectBooksRead()
{
Set<String> readBooks =new HashSet<String>();
String title;
{
do
{
title = OUDialog.request("Please input a book title or * to finish","");//OUDialog.request displays an input dialogue box as specified by a superclass
readBooks.add(title);
}
while
(title !="*");
}
> I'm having difficulty with the following question for
> an OU exam:
>
> You will now create a method to enable students to
> make a list of the books they have read, whether they
> are on the courses reading list or not.
> Add an instance method to the class, with the header:
> public Set<String> collectBooksRead()
> The method should first declare a local variable of
> type Set called readBooks capable of holding a set of
> strings.
> The method should then assign to that variable an
> appropriate set object.
> The method should then use a dialogue box to collect
> the book titles one at a time from the user.
> The dialogue box should have the prompt "Please input
> a book title or * to finish", and should have an
> empty string as its default reply.
> Each entered book title should be added to the set
> referenced by the local variable readBooks. Finally
> the method should return readBooks as the message
> answer.
>
> Here's my code so far but it endlessly loops and I'm
> unsure how to make it stop.
>
unplug the computer. works every time
thanks for that...but can i have a serious reply
> thanks for that...but can i have a serious reply
Maybe "*" was never entered as title? does this compile?
Message was edited by:
snic.snac
// right
str1.equals(str2)
!str1.equals(str2)
// wrong
str1 == str2
str1 != str2
Do you understand why?
jverda at 2007-7-29 13:12:34 >

Have tried entering both an asterix on its own and in the double quotes but loop continues in the same way.
on entering * into the user input Dialogue box, the dialogue should close and the whole list of titles entered should then be output as text
is there some form of code to close a dialogue box on the fulfillment of a given criteria?
http://forum.java.sun.com/thread.jspa?threadID=5196757
~
> Have tried entering both an asterix on its own and in
> the double quotes but loop continues in the same way.
>
> n entering * into the user input Dialogue box, the
> dialogue should close and the whole list of titles
> entered should then be output as text
>
> is there some form of code to close a dialogue box on
> the fulfillment of a given criteria?
See reply 4.
jverda at 2007-7-29 13:12:35 >

Thanks.. this works a charm now.
Take it its to do with the syntax of comparing objects. but don't really understand why
> Thanks.. this works a charm now.
>
> Take it its to do with the syntax of comparing
> objects. but don't really understand why
The == operator compares the values of its operands. If the operands are reference types (in this case String types), it compares reference values (*not* the contents of the String). Reference values are pointers to objects (in this case, different String objects). Because the references point to different objects, their values -- naturally -- are different, so your expression never evaluates to "true".
~
> Thanks.. this works a charm now.
>
> Take it its to do with the syntax of comparing
> objects. but don't really understand why
== compares reference values. That is, do both references point to the same object (or do both contain null).
equals() does whatever that particular class defines it to do. In the case of String, in compares the sequence of characters held in the String objects.
When you do input == "*", it's false, because even though the String object pointed to by the input variable and the String object pointed to by the literal "*" contain the same sequence of characters (the single char '*'), they're two distinct objects.
jverda at 2007-7-29 13:12:35 >

many thanks again... understand the uses of these now.