Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Creating new objects within methods

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating new objects within methods

    I want to make a program where users are prompted to enter a username and a password and have these two values create a new instance of the Object User. But I'm not sure where to start. Can anyone point me in the right direction?

    import java.util.Scanner;
     
     
    public class Main {
    	public static void main(String[] args) {
    		createUser();
     
    	}
    	public class User
    	{
    		String username;
    		String password;
    	}
    	public static void createUser()
    	{
    		String username;
    		String password;
    		Scanner getPassword = new Scanner(System.in);
    		Scanner getUsername = new Scanner(System.in);
    		System.out.println("Set a username.");
    		username = getUsername.nextLine();
    		System.out.println("Set a password.");
    		password = getPassword.nextLine();
    		System.out.println(username + " " + password);
     
    	}
     
    }
    I'm just not sure how to take username + password and put it into an object.


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Creating new objects within methods

    How about prompting the user in main to enter in a username and password. Then use the username and password in the constructor of the object you create.

    example

    enter in username
    //user enters in name
    enter in password
    //user enters in password

    object = new object(username, password)

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating new objects within methods

    Quote Originally Posted by camel-man View Post
    How about prompting the user in main to enter in a username and password. Then use the username and password in the constructor of the object you create.

    example

    enter in username
    //user enters in name
    enter in password
    //user enters in password

    object = new object(username, password)
    Thats what I posted in the OP lol.
    I just don't know how to take the user input and enter it into a method that creates the object and assigns the object a name like User1, then the next created user to be named User2 etc.

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Creating new objects within methods

    create an arrayList of objects. Then use the constructor for the usernames and passwords.

    ArrayList<blah> t = new ArrayList<blah>();
     
    	t.add(new blah(/*the username , the password*/));

    You could put it in a loop for as many times as you want.
    Last edited by camel-man; September 14th, 2014 at 11:28 PM.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating new objects within methods

    Quote Originally Posted by camel-man View Post
    create an arrayList of objects. Then use the constructor for the usernames and passwords.

    ArrayList<blah> t = new ArrayList<blah>();
     
    	t.add(new blah(/*the username , the password*/));

    You could put it in a loop for as many times as you want.
    I'm sorry man I don't have a solid foundation in programming or Java, so I'm not even sure what you posted.
    Tell me if this is right or wrong.
    Where you have blah, my array values will be? Or will User go there?

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Creating new objects within methods

    "blah" in my case is the data type. In your case it would be User. You might want to put User in another class instead of having it in main.

    public class User
    	{
    		String username;
    		String password;
     
             public User(){
                  createUser();
              }
    	public void createUser()
    	{
    		Scanner getInfo = new Scanner(System.in);
    		System.out.println("Set a username.");
    		username = getInfo.nextLine();
    		System.out.println("Set a password.");
    		password = getInfo.nextLine();
    		System.out.println(username + " " + password);
     
    	}
    }

    This is how your User class should look. Took out a few redundant things and this should be in another class file (separate from Main). Makes it easier to keep neat and clean.

Similar Threads

  1. Creating objects from methods
    By fahman_khan75@yahoo.com in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 21st, 2014, 12:00 PM
  2. [SOLVED] Help with objects, methods, and class inheritance.
    By Gingerbread Fetus in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2013, 09:02 AM
  3. [SOLVED] Creating an array of objects
    By Benner in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 22nd, 2013, 03:00 AM
  4. Creating random objects
    By JackCannon15 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 18th, 2011, 07:50 AM
  5. [SOLVED] Word Scramble Using Objects & Methods
    By Whitechapel in forum Object Oriented Programming
    Replies: 3
    Last Post: May 23rd, 2010, 12:38 PM