Java - How to create a class instance from user input

22,839

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());
}
Share:
22,839
Gregg1989
Author by

Gregg1989

Updated on February 17, 2020

Comments

  • Gregg1989
    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
      Taylor over 10 years
      "and create a class instance with a name equal to the input of the user" What does that mean?
    • Gregg1989
      Gregg1989 over 10 years
      If 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
      zapl over 10 years
      The reason why you can't do that is because you try to assign a createMember object to a String variable.
    • Charles Forsythe
      Charles Forsythe over 10 years
      You can't assign a createMember instance to line because line is declared as a String. If you don't understand that, then you have to learn more about Java and strong typing.
    • Arash Saidi
      Arash Saidi over 10 years
      What you are doing with the input variable is not allowed. input is a string, new createMember() creates an instance of the class createMember
    • Gregg1989
      Gregg1989 over 10 years
      zapl, not sure exactly what you're trying to tell me. How do I fix the issue?
    • Francis
      Francis over 10 years
      your 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
      MadConan over 10 years
      What you are asking is possible. However, the output/end-state you are looking for doesn't require dynamic type creation.
    • Artem Moskalev
      Artem Moskalev over 10 years
      No, 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
      Taylor over 10 years
      To 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
    azz over 10 years
    Note the createMember/CreateMember inconsistency.