0

I need help with a method. The method I've written is:

public void readBook(String bookReader, String book);

The method needs to show both strings of the argument in the console. What else do I need to do to get the method to work?

6
  • Is that a method ? It looks like a declaration to me. Can you publish the complete method implementation ? Commented Feb 8, 2010 at 23:09
  • sorry, what i ment was ive defined the method (above) but how do i get the method to print the strings (of the argument) in the console? Commented Feb 8, 2010 at 23:13
  • 1
    Is this homework? Are you asking us to write it for you? Commented Feb 8, 2010 at 23:13
  • 1
    nope im trying to learn how to write methods Commented Feb 8, 2010 at 23:14
  • 1
    Sun has some great beginning Java tutorials here, probably best to start with the documentation: java.sun.com/docs/books/tutorial/java/index.html Commented Feb 8, 2010 at 23:16

5 Answers 5

8
public void readBook(String bookReader, String book){
    System.out.println("Book Reader: " + bookReader);
    System.out.println("Book:" + book);
}

This should do it. I recomend google for questions like this though.

Sign up to request clarification or add additional context in comments.

1 Comment

In this case, it's not about googling for answers, but actually learning Java.
3

Once you've done the above i.e.

public void readBook(String bookReader, String book){ 
    System.out.println("Book Reader: " + bookReader);
    System.out.println("Book:" + book); 
}

...you need to call it from your main method (or wherever you want) like so:

readBook("The Book Reader", "The Books Name");

Comments

2

in your method body, use

System.out.println( bookReader );
System.out.println( book );

For printing them on separate lines

3 Comments

thanks mate, i was thinking about that but didnt know if it was possible to do it with methods
@noob11 Looks like you got a lack of some fundamental concepts here.
@Willi: Everybody starts somewhere
0

If all you want is to print the variables, try :

String SPACE = " ";  // can make it class level constant.
String separator = SPACE;  // can have different string, space is just an example
System.out.println(bookReader + separator + book);

There are lot of ways to embelish it.

Comments

0

There are two ways to print to the console

First:

System.out.print("Your string here");

Second:

System.out.println("Your string here");

The difference between the two is that the second will automatically place the output on a new line in the console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.