| Author |
Can a method that has a parameter call another method that has a paramter?
|
Ashley Kin
Ranch Hand
Joined: Oct 18, 2011
Posts: 36
|
|
I'm trying to do this, but I keep getting errors such as "illegal start of expression" or ") expected"
This code is supposed to run through a game the number of times that a user inputs.
I have a method for what a user enters as their "weapon" (i.e rock, paper, scissors) and returns the string weapon and another method for what the computer randomly chooses, which returns cWeapon.
Then I have another method to print who won based on these two weapons.
Should I be approaching this a different way other than methods with parameters?
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
|
would you mind pasting the whole code?
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Yes a method that has a parameter can call another method that has a parameter. It's not dependent on that.
Best example I can think of is the main method that has a (String[] args) parameter. And you use that to call other methods (how did you call your games(int) method?)
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
You have to store the weapons returned by the userWeapon() and singleMatch() methods to send it as input to the whoWon() method. Your code misses that part... or at least the code you pasted
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
John Jai wrote:
You don't have to specify the type when you call the method; leave of the "String".
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Greg Brannon
Bartender
Joined: Oct 24, 2010
Posts: 530
|
|
|
The compiler is confused whether line 9 is a method call or a method signature. If it is a method call, then omit the parameter types.
|
Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
Jesper de Jong wrote:
John Jai wrote:
You don't have to specify the type when you call the method; leave of the "String".
I thought he didn't have the variables weapon and cWeapon declared previously
|
 |
Harsha Smith
Ranch Hand
Joined: Jul 18, 2011
Posts: 287
|
|
You are creating too many scanner objects by using for loop unnecesarily . all you have to do is declare a field like this
which can be used by all the methods in the class.
Assuming that your class has a "static" whoWon(String userWeapon, String singleMatchWeapon) method, as told by others, you have call this method just like you called userWeapon and singleMatch methods in the loops
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Why have you got all those members static? It is a good idea to have the Scanner object static, But having a static games() method looks suspicious to me. Also you would do better to get the details from the Scanner, store them as some sort of variable, and pass that variable to the method. Rather thantry... or something similar.
An alternative approach: Set up a utility class, like this, with a static Scanner field, like what Harsha Smith showed earlier. Rather than the multiplying method I showed, create methods like public static int nextIntFromKeyboard(...); you can see what such a method contains here. Remember that is not a complete method. If you ever use System.in or similar as a parameter to a Scanner or similar, don’t simply close the Scanner. Otherwise you close System.in and can’t reopen it
|
 |
 |
|
|
subject: Can a method that has a parameter call another method that has a paramter?
|
|
|