java - Creating objects from user input - constructor requires parameters of different types -



java - Creating objects from user input - constructor requires parameters of different types -

background: i've been asked create project involves multiple classes store different information, followed menu allows users create objects of each class , phone call various methods each (i.e. 'average of numbers stored' etc.)

i've set 'menu' class, scanner allowing user input, prints list of keywords users type in select option. 'type 'classa' create classa' etc. i've stored array of possible keywords. user input stored in instance variable 'keyword' , checked against array accuracy.

once it's confirmed, want set 'if' loop phone call method each possible keyword. example

private void processkeyword() { string keyword = getkeyword(); if (keyword.equals("keyword1")) { createclass1(); } if (keyword.equals("keyword2")) { class1.method1(); }

etc etc. can set private methods within menu class allow me these things.

problem: problem have of constructors , methods require parameters of both integer , string type. scanner stores input strings, , asking parameters straight method (let's createclass1()) stops me calling if loop.

anybody have ideas how can around roadblock?

well, don't give lot go on, here's simplistic answer.

just create more local variables classes create. phone call them when needed.

private void processkeyword() { string keyword = getkeyword(); class1 aclass; // variable here if (keyword.equals("keyword1")) { aclass = createclass1(); // must assigned } if (keyword.equals("keyword2")) { aclass.method1(); // phone call }

probably want phone call new class1() instead of createclass1() can pass parameters constructor. that's next step -- calling ctors different parameters. first think should 1 class working 1 type of parameter. type like, whatever seems easiest.

then post new question , you've got. explain you're going these new parameters (you don't in op). isn't hard, little tedious different possibilities. stick , done.

java

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -