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?
-
Is that a method ? It looks like a declaration to me. Can you publish the complete method implementation ?Brian Agnew– Brian Agnew2010年02月08日 23:09:59 +00:00Commented 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?user267935– user2679352010年02月08日 23:13:08 +00:00Commented Feb 8, 2010 at 23:13
-
1Is this homework? Are you asking us to write it for you?Jonathon Faust– Jonathon Faust2010年02月08日 23:13:50 +00:00Commented Feb 8, 2010 at 23:13
-
1nope im trying to learn how to write methodsuser267935– user2679352010年02月08日 23:14:25 +00:00Commented Feb 8, 2010 at 23:14
-
1Sun has some great beginning Java tutorials here, probably best to start with the documentation: java.sun.com/docs/books/tutorial/java/index.htmlJonathon Faust– Jonathon Faust2010年02月08日 23:16:39 +00:00Commented Feb 8, 2010 at 23:16
5 Answers 5
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.
1 Comment
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
in your method body, use
System.out.println( bookReader );
System.out.println( book );
For printing them on separate lines
3 Comments
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
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.