Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

passing arguments to a constructor  RSS feed

 
Toyin Charles
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A real beginner here

I need to know to pass "keyboard input" as arguments to an instantiated object. This object would send the arguments back through a method call for processing (hope I asked the question right?). Example:

class Test {

public static void main(String [] args) {

SomeClass anotherClass = new SomeClass("Me Too", Cool, "Twenty");

anotherClass.displayStuff();//
}
}

//SomeClass has a valid constructor
//Called method exists and just print
//Output is:
//Me Too
//Cool
//Twenty
.....
**This works fine. What is I want to input the variables instead?
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch . . .

I can think of several ways to do it:
  • Use command-line arguments: . . . new SomeClass(args[0], args[1], args[2]); But you must make sure to pass three command-line arguments.
  • A java.util.Scanner object with System.in passed to its constructor. . . . new SomeClass(inScan.next(), inScan.next(), inScan.next()); You won't get any response until you have entered all three Strings. The default Scanner divides words by whitespace.
  • Use the static showInputDialog() method of javax.swing.JOptionPane. If you are not in a GUI application, pass "null" as the first argument. You get one String at a time.
  • And there are bound to be more ways.
     
    Toyin Charles
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes I'm using the Scanner class (imported already: import java.util.Scanner
    The code below is in the same method as the other code.





    The other code becomes;




    The confusion is that the code compiles, but outputs the initialized value, and not the user input.
    anotherClass.displayStuff(); should receive the arguments and use them to display the user input.

    Appreciate pointing me in the right direction.
     
    Campbell Ritchie
    Marshal
    Posts: 55715
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am surprised you are not getting the input returned.
    Suggest you take off the original initialisations, which are unnecessary. ChangetoI presume you have all that lot in the same method?

    I have tried out what you are describing, and entering
    Campbell
    93847593479
    Ritchie
    causes it to print
    Campbell
    93847593479
    Ritchie

    That appears to be what you wanted. You are obviously doing something you haven't yet described. Are you creating anotherClass (BTW: the names you are giving your objects and classes are very poor) twice? It is very awkward tryin gto work out what you are doing when you only post part of your code.
    This is how I think you are trying to do it:Only I have changed some of the names. Please go through that and see where it differs from what you have got.

    And it is late here; I am off to bed.
     
    Nicholas Jordan
    Ranch Hand
    Posts: 1282
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Toyin Charles:
    The other code becomes;

    The confusion is that the code compiles, but outputs the initialized value, and not the user input.
    anotherClass.displayStuff(); should receive the arguments and use them to display the user input.
    Appreciate pointing me in the right direction.


    Note where Campbell Ritchie puts ScannerDemo anotherClass = new ScannerDemo(textOne, numOne, textTwo);.

    This very likely has a lot to do with it based on your description of what is not working the way you want it.
     
    Campbell Ritchie
    Marshal
    Posts: 55715
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Note where I put it? [As Nicholas Jordan said.]

    Is that what you are doing? I thought it might be. That is why you had to initialise the local variables.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I also agree and suspect whether instantation of SomeClass object is handled before OR after getting the inputs from keyboard.

    If it is before, then obviously thats where the mistake and the keyboard inputs would NOT have any effects.

    It would be better if you could paste the complete code Toyin Charles. Moreover it would be helpful to get a correct solution at the earliest.

    Good luck
     
    Toyin Charles
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Great guys! You're amazing!!
    Sorry for the delay - time zone differences.

    This is now resolved, thanks to all your input. Sorry for posting only some of my code, i didn't know it made understanding difficult (will post full codes in future). The object and class names are not the actual - I'll use more meaningful names in future (I'm learning minute).

    Anyway, Campbell Ritchie set the ball rolling by providing a good code template and Nicholas Jordan exposed what Raghavan Muthu made clear to me. I had no idea the effect wrongly locating instantiated objects had! I had gracefully created new objects at the beginning of the method and then placed keyboard input afterwards - reversal worked perfect.

    I wonder how come it was so clear to you guys without seeing my full code? Surely, I'll hang out here more often - you guys see things.

    Thanks, and sorry for the long post of text, I'm only impressed!
     
    Campbell Ritchie
    Marshal
    Posts: 55715
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How was it so clear to us?

    Experience!

    Only too pleased to help
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!