• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?




 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would you mind pasting the whole code?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic