Too many toStrings() !!!

2 Questions:

1)
I'm writing a program to simulate a small library. One class has a toString() method which overrides the one in the String class and prints out data in a certain form:
idnum|title|author|copies|size_of_recall_list
with the vertical lines being characters in the string to be returned. I know that the idnum uses the handy Integer.toString(int a) method but in order to print out the entire thing (idnum|title|author|copies|size_of_recall_list) do I need to convert it all into a string using the String class toString()? Or can just say
idnum + "|" + title + "|" +...?

2)another method in the same class called status() calls the aforementioned toString() (the overriding one) and then creates a new line for every patron who is in the recall list and prints their name. I'm not quite sure how to go about tackling that. I thought I could use /n (or
) or even
but it doesn't recognize it. And I'm not sure how I'm supposed to return a multi-line string...I've never seen that done before. Any help? Thanks. Owen

Comments

  • : 2 Questions:
    :
    : 1)
    : I'm writing a program to simulate a small library. One class has a toString() method which overrides the one in the String class and prints out data in a certain form:
    : idnum|title|author|copies|size_of_recall_list
    : with the vertical lines being characters in the string to be returned. I know that the idnum uses the handy Integer.toString(int a) method but in order to print out the entire thing (idnum|title|author|copies|size_of_recall_list) do I need to convert it all into a string using the String class toString()? Or can just say
    : idnum + "|" + title + "|" +...?
    :
    [green]
    I think what you are doing is saying idnum = Integer.toString(int a), and similar for any other number in the list of data. What then happens is that idnum is a string that happens to have as its characters a bunch of digits. In that case, to print out the whole thing you would do as you said, idnum + "|" + title ...
    [/green]

    : 2)another method in the same class called status() calls the aforementioned toString() (the overriding one) and then creates a new line for every patron who is in the recall list and prints their name. I'm not quite sure how to go about tackling that. I thought I could use /n (or
    ) or even
    but it doesn't recognize it. And I'm not sure how I'm supposed to return a multi-line string...I've never seen that done before. Any help? Thanks. Owen
    :
    [green]
    The trick I use for putting newline characters into a string is to add them as characters. The character '
    ' is a newline (as you mentioned above), and there is also a numerical value for it. Create a constant called newline that is that character, and everytime you want a newline, you just add it to the end, such as s1 + ENTER + s2 + ENTER + ... and then return that String. You can try just making a simple program that prints out a String containing this character, and seeing what it does.
    [/green]

    There are two methods in software design. One is to make the program so simple, there are obviously no errors. The other is to make it so complicated, there are no obvious errors.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion