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.

10
  • "and create a class instance with a name equal to the input of the user" What does that mean? Commented Nov 5, 2013 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. Commented Nov 5, 2013 at 19:06
  • The reason why you can't do that is because you try to assign a createMember object to a String variable. Commented Nov 5, 2013 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. Commented Nov 5, 2013 at 19:07
  • 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. Commented Nov 5, 2013 at 19:09

2 Answers 2

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");
        }
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

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());
}

1 Comment

Note the createMember/CreateMember inconsistency.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.