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?

BalusC
1.1m377 gold badges3.7k silver badges3.6k bronze badges
asked Feb 8, 2010 at 23:07
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.

answered Feb 8, 2010 at 23:14
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");
answered Feb 9, 2010 at 11:58

Comments

2

in your method body, use

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

For printing them on separate lines

answered Feb 8, 2010 at 23:15

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.

answered Feb 9, 2010 at 12:12

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.

answered Feb 9, 2010 at 12:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.