• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Pass a variable to a method in another class, then pass back

 
Ranch Hand
Posts: 42
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a variable, month, in a method, inputSystemTask(), which is in class, SystemTask.


In another class, UserInput, I have a method, getString(int minString, int maxString).


I need to pass the variable, month, to method, getString, then have it come back to method inputSystemTask().

Thank you.
 
Ranch Hand
Posts: 47
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can simply add this variable as a third parameter to your "getString" method - and as you return the string from it, you will receive it back after call is completed (if it is the string you want).

However I dare say that your code is not looking quite proper - or at least I could not make good sense of it. For example getString recursively calls itself - I believe it is not what you wanted to do. You wanted to use loop for user to be able to retry - and as you input the string in the inputSystemTask - that is where your loop also should be. Your getString really is not "getString" but more like "checkString".
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott M Summers wrote:In another class, UserInput, I have a method, getString(int minString, int maxString).
I need to pass the variable, month, to method, getString, then have it come back to method inputSystemTask().
Thank you.


Scott,

When you post code, you really should try to give us the full picture. It would appear that your UserInput class ALSO has a method called getInput() that takes no arguments, but you haven't shown it, which is a bit confusing for us.

I'm also not quite sure why you would need to pass a "month" to an input method.

I'm guessing here, but I assume that what you want to do is have the user enter a month, and then validate that it is between 1 and 12 - or maybe between 1 and some "current month", and you want to know how to do that.

If I'm right, then here's my suggestion:
Write a method in your UserInput class, that forces the user to enter a number within certain limits (getNumberInRange(...) ?).
Then, to get a valid month, all you need to do is call:
    getNumberInRange(1, 12)

You may also find the UserInput page worth reading. It's fairly long, and the third part still isn't finished; but it might give you some pointers.

Winston
 
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest you enhance that user input method. It is a good idea to write that sort of utility class to get your Scanner working at full power. But you can make the length check into a loop continuation condition. Then if the input is of the wrong length, you can print a message with something like “try again” included, and make the user repeat the input. That will keep going until you get the correct size String.

You haven't indented your code correctly. Your methods should start and finish farther to the right than the class starts.
 
Campbell Ritchie
Marshal
Posts: 80780
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what your class should have looked like. Note one blank line between pairs of methods. Also if you use K&R indentation the { should be separated from the preceding text; I have used one space each time. Long comments deleted. Space before and after binary operators like ==:-
I don't usually use K&R indentation so you might do well, if you have the time, to read about the exact number of spaces suggested. That will probably vary from source to source so find one convention you like and stick to that.
Leave out comments like month set method, which don't tell us anything we don't already know.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic