• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help clarifying this instruction involving static methods?  RSS feed

 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a static method getInput to prompt for and read in the input.  Be sure to validate the input before returning it.  There should be NO negative values allowed.  Use a do-while loop for validation.  The method should look like this:               public static double getInput(String type)

To get miles, for example, you would call the method with the argument “miles”.  Use the type in the prompt.  The method will return the user input.

I'm confused by this. How would I write the static method itself? Do I write public static double getInput(String type) then have the scanner object and get the user input in the body? When I invoke this method, do I write double someVariable = getInput("miles");
Do I need to have "miles" somewhere in the body of the actual static method? I'm not sure how to set this up? Part of the assignments is converting different units of measurements and we need to use the getInput method to get the user input from all of them then perform the conversion. I guess I'm just not sure how to set up the static method itself. This is what I wrote at first but I'm sure it's all wrong.


 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrea Menjivar wrote:. . . Do I write public static double getInput(String type)
Yes
then have the scanner object
No. Make the Scanner object a private static final field of this class. You are creating a utility method for keyboard input. Only use the one Scanner object and never close it. There is no need to create a new Scanner every time you call that method.
and get the user input in the body? When I invoke this method, do I write double someVariable = getInput("miles");
Yes. But if the method is in a different class, it will be
double miles = KeyboardInputs.getInput("miles");
I think it would have been better to have called the method getDouble or getNonNegativeDouble.
Do I need to have "miles" somewhere in the body of the actual static method?
No. You will probably start the method like this:-
System.out.printf("Please enter %s, no negative values: ", type);
I'm not sure how to set this up? Part of the assignments is converting different units of measurements and we need to use the getInput method to get the user input from all of them then perform the conversion.
The getInput method is for getting the input and nothing else. You can do the conversions in a different method. You can think about conversions later.
I guess I'm just not sure how to set up the static method itself. This is what I wrote at first but I'm sure it's all wrong. . . .
No, it isn't wrong at all. I would move the Scanner creation out of the method, as I said earlier, and I would include a line to print the request, but otherwise that will work.
If you are using doubles, write < 0.0 rather than < 0.

I think the name of the method s‍hould be something like getNonNegativeDouble and the parameter shou‍ld be something like unit, but otherwise I think that method will work I think you shou‍ld put that sort of utility method into a class of its own, with other static methods like getInt, and you can then use that class again. I have a class like that called KeyboardInputs. Utility classes are allowed plural forms for their names.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!