Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a method that has a parameter call another method that has a paramter?

 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would you mind pasting the whole code?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 15494
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:

You don't have to specify the type when you call the method; leave of the "String".
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50267
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic