2

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.

Gregg1989
  • 675
  • 6
  • 14
  • 24
  • "and create a class instance with a name equal to the input of the user" What does that mean? – Taylor Nov 05 '13 at 19:06
  • 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. – Gregg1989 Nov 05 '13 at 19:06
  • The reason why you can't do that is because you try to assign a `createMember` object to a `String` variable. – zapl Nov 05 '13 at 19:07
  • 3
    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. – Charles Forsythe Nov 05 '13 at 19:07
  • What you are doing with the input variable is not allowed. input is a string, new createMember() creates an instance of the class createMember – Arash Saidi Nov 05 '13 at 19:07
  • zapl, not sure exactly what you're trying to tell me. How do I fix the issue? – Gregg1989 Nov 05 '13 at 19:07
  • 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. – Francis Nov 05 '13 at 19:07
  • What you are asking is possible. However, the output/end-state you are looking for doesn't require dynamic type creation. – MadConan Nov 05 '13 at 19:08
  • No, it is actually possible with reflection. I have never tried to, but you can construct classes on the fly. Here was the discussion: http://stackoverflow.com/questions/9125583/how-to-create-java-classes-dynamically-by-java-reflection – Artem Moskalev Nov 05 '13 at 19:09
  • 2
    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. – Taylor Nov 05 '13 at 19:09

2 Answers2

5

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");
        }
    }
} 
azz
  • 5,852
  • 3
  • 30
  • 58
1

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());
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720