Java - How to create a class instance from user input
Solution 1
Looks like you need a non-default Constructor: (Constructors CANNOT return any value, not even void, as the instance is what is actually returned.
String input = keyboard.nextLine();
Member m = new Member(input);
public class Member {
private String name;
public Member(String name) {
this.name = name;
}
public void setMembership() {
try {
//Creates a text file with the same name as the username where data is stored.
Formatter x = new Formatter(name);
} catch (Exception e) {
out.println("Could not create username");
}
}
}
Solution 2
You need a constructor
public class CreateMember {
private String input;
public CreateMember(String input){
this.input = input;
}
public String getInput(){
return input;
}
}
To access the input use CreateMember.getInput()
public static void main(String[] args){
String input = scanner.nextLine();
CreateMember member = new CreateMember(input);
System.out.println(member.getInput());
}
Gregg1989
Updated on February 17, 2020Comments
-
Gregg1989 over 4 years
I want this program to ask the user for input, and create a class instance with a name equal to the input of the user. Then, the createMember class will create a text file, where all the data of the user will be stored. How do I go about doing it?
Here's the main method:
public static void main(String[] args) { String input = keyboard.nextLine(); input = new createMember(); // Error. can't do that for some reason? } }
Here's the createMember class
public class createMember { public void setMembership() { Scanner keyboard = new Scanner(System.in); out.println("Username: "); String input = keyboard.nextLine(); try { //Creates a text file with the same name as the username where data is stored. Formatter x = new Formatter(input); } catch (Exception e) { out.println("Could not create username"); } } //Methods for the user
Guys... I know I can simply create an instance like this:
createMember member = new createMember();
What I actually want to do is HAVE THE USER do that on his own, so the program is flexible and usable for many people. So, based on the input, there will be a separate folder that stores the data for each user.
-
Taylor over 10 years"and create a class instance with a name equal to the input of the user" What does that mean?
-
Gregg1989 over 10 yearsIf the user enters Bob, the instance of the class will be called Bob, and there will be a .txt file called Bob that stores all the data of that instance.
-
zapl over 10 yearsThe reason why you can't do that is because you try to assign a
createMember
object to aString
variable. -
Charles Forsythe over 10 yearsYou can't assign a
createMember
instance toline
becauseline
is declared as aString
. If you don't understand that, then you have to learn more about Java and strong typing. -
Arash Saidi over 10 yearsWhat you are doing with the input variable is not allowed. input is a string, new createMember() creates an instance of the class createMember
-
Gregg1989 over 10 yearszapl, not sure exactly what you're trying to tell me. How do I fix the issue?
-
Francis over 10 yearsyour variable name should not change during runtime. That doesn't make sense. You might want to have a username property in your class instead.
-
MadConan over 10 yearsWhat you are asking is possible. However, the output/end-state you are looking for doesn't require dynamic type creation.
-
Artem Moskalev over 10 yearsNo, it is actually possible with reflection. I have never tried to, but you can construct classes on the fly. Here was the discussion: stackoverflow.com/questions/9125583/…
-
Taylor over 10 yearsTo echo what @CharlesForsythe said above, you seem to have some misconceptions about Java and programming in general. I suggest you go over some basic concepts first.
-
-
azz over 10 yearsNote the
createMember
/CreateMember
inconsistency.