Creating Objects while program runs












0















I'd like to create a console application, where there should be a possibility to create Objects while the program is running. My first attempts looked like this:



InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
printMenu();
String input = br.readLine();
switch (input) {
case "0":
System.exit(0);
case "1":
createStudent();
(...)


createStudent():



String firstName;
String lastName;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String input = br.readLine();
System.out.println("whats your Lastname?");
input = br.readLine();
lastName = input;
System.out.println("and your Firstname?");
input = br.readLine();
firstName = input;
// Create Object with given attributes
Student unique = new Student(firstName,lastName);


The whole Application is based on user inputs. I need to be able to create multiple Students with diferent names (in my code the Name of the Object will always be "unique".










share|improve this question




















  • 4





    What isn't working for you? What are your expected results?

    – Ryan Wilson
    Nov 21 '18 at 20:13











  • it is working the problem is that i'd like to have different names for "unique", for example the name of the Student should be the name of the object... smth like: Student firstName = new Student(..);

    – Hecko2g
    Nov 21 '18 at 20:21











  • unique is one object, are you storing it in a List<T> of type Student or what are you doing with it once it is created?

    – Ryan Wilson
    Nov 21 '18 at 20:24


















0















I'd like to create a console application, where there should be a possibility to create Objects while the program is running. My first attempts looked like this:



InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
printMenu();
String input = br.readLine();
switch (input) {
case "0":
System.exit(0);
case "1":
createStudent();
(...)


createStudent():



String firstName;
String lastName;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String input = br.readLine();
System.out.println("whats your Lastname?");
input = br.readLine();
lastName = input;
System.out.println("and your Firstname?");
input = br.readLine();
firstName = input;
// Create Object with given attributes
Student unique = new Student(firstName,lastName);


The whole Application is based on user inputs. I need to be able to create multiple Students with diferent names (in my code the Name of the Object will always be "unique".










share|improve this question




















  • 4





    What isn't working for you? What are your expected results?

    – Ryan Wilson
    Nov 21 '18 at 20:13











  • it is working the problem is that i'd like to have different names for "unique", for example the name of the Student should be the name of the object... smth like: Student firstName = new Student(..);

    – Hecko2g
    Nov 21 '18 at 20:21











  • unique is one object, are you storing it in a List<T> of type Student or what are you doing with it once it is created?

    – Ryan Wilson
    Nov 21 '18 at 20:24
















0












0








0








I'd like to create a console application, where there should be a possibility to create Objects while the program is running. My first attempts looked like this:



InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
printMenu();
String input = br.readLine();
switch (input) {
case "0":
System.exit(0);
case "1":
createStudent();
(...)


createStudent():



String firstName;
String lastName;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String input = br.readLine();
System.out.println("whats your Lastname?");
input = br.readLine();
lastName = input;
System.out.println("and your Firstname?");
input = br.readLine();
firstName = input;
// Create Object with given attributes
Student unique = new Student(firstName,lastName);


The whole Application is based on user inputs. I need to be able to create multiple Students with diferent names (in my code the Name of the Object will always be "unique".










share|improve this question
















I'd like to create a console application, where there should be a possibility to create Objects while the program is running. My first attempts looked like this:



InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
printMenu();
String input = br.readLine();
switch (input) {
case "0":
System.exit(0);
case "1":
createStudent();
(...)


createStudent():



String firstName;
String lastName;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String input = br.readLine();
System.out.println("whats your Lastname?");
input = br.readLine();
lastName = input;
System.out.println("and your Firstname?");
input = br.readLine();
firstName = input;
// Create Object with given attributes
Student unique = new Student(firstName,lastName);


The whole Application is based on user inputs. I need to be able to create multiple Students with diferent names (in my code the Name of the Object will always be "unique".







java object input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 21:54









Mihai Chelaru

2,417101323




2,417101323










asked Nov 21 '18 at 20:10









Hecko2gHecko2g

81




81








  • 4





    What isn't working for you? What are your expected results?

    – Ryan Wilson
    Nov 21 '18 at 20:13











  • it is working the problem is that i'd like to have different names for "unique", for example the name of the Student should be the name of the object... smth like: Student firstName = new Student(..);

    – Hecko2g
    Nov 21 '18 at 20:21











  • unique is one object, are you storing it in a List<T> of type Student or what are you doing with it once it is created?

    – Ryan Wilson
    Nov 21 '18 at 20:24
















  • 4





    What isn't working for you? What are your expected results?

    – Ryan Wilson
    Nov 21 '18 at 20:13











  • it is working the problem is that i'd like to have different names for "unique", for example the name of the Student should be the name of the object... smth like: Student firstName = new Student(..);

    – Hecko2g
    Nov 21 '18 at 20:21











  • unique is one object, are you storing it in a List<T> of type Student or what are you doing with it once it is created?

    – Ryan Wilson
    Nov 21 '18 at 20:24










4




4





What isn't working for you? What are your expected results?

– Ryan Wilson
Nov 21 '18 at 20:13





What isn't working for you? What are your expected results?

– Ryan Wilson
Nov 21 '18 at 20:13













it is working the problem is that i'd like to have different names for "unique", for example the name of the Student should be the name of the object... smth like: Student firstName = new Student(..);

– Hecko2g
Nov 21 '18 at 20:21





it is working the problem is that i'd like to have different names for "unique", for example the name of the Student should be the name of the object... smth like: Student firstName = new Student(..);

– Hecko2g
Nov 21 '18 at 20:21













unique is one object, are you storing it in a List<T> of type Student or what are you doing with it once it is created?

– Ryan Wilson
Nov 21 '18 at 20:24







unique is one object, are you storing it in a List<T> of type Student or what are you doing with it once it is created?

– Ryan Wilson
Nov 21 '18 at 20:24














2 Answers
2






active

oldest

votes


















1














You need to use a collection to store your students! The most simple is an array:



Student students = new Student[100];


This will create an array for maximum 100 Students. One problem with this is that it cannot be resized, so if you add more then 100, new arrays need to be created, and copied which is not easy.



You should use a collection which has no fixed size: Any one from java.utils will do, like ArrayList, LinkedList, Stack, ...



LinkedList<Student> students = new LinkedList<Student>();





share|improve this answer































    0














    You will need to create an array list of Student objects. You could add an unlimited number of Student objects to the array list.



    I played around with it a bit, and I figured out how to add objects to the ArrayList. Here is an example of how you could do this:



    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;

    public class Students {

    ArrayList<Student> unique = new ArrayList<Student>();

    public void createStudent() throws IOException {

    String firstName = "";
    String lastName = "";
    Student temp;

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);

    System.out.println("whats your Lastname?");
    lastName = br.readLine();

    System.out.println("and your Firstname?");
    firstName = br.readLine();

    // Create Object with given attributes
    temp = new Student(firstName, lastName);
    unique.add(temp);
    }

    public String getFirstName(int index) {
    return unique.get(index).firstName;
    }

    public String getLastName(int index) {
    return unique.get(index).lastName;
    }

    public static void main(String args) throws IOException {

    Students students = new Students();
    students.createStudent();

    System.out.println(students.getFirstName(0));
    System.out.println(students.getLastName(0));
    }
    }

    class Student {

    String firstName = "";
    String lastName = "";

    public Student(String fn, String ln) {
    firstName = fn;
    lastName = ln;
    }
    }


    Let me know if you need any more help.






    share|improve this answer


























    • And how do I use this ? I mean when I have this line of code, how do I assign the attributes to the Object ?

      – Hecko2g
      Nov 21 '18 at 20:41












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419781%2fcreating-objects-while-program-runs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You need to use a collection to store your students! The most simple is an array:



    Student students = new Student[100];


    This will create an array for maximum 100 Students. One problem with this is that it cannot be resized, so if you add more then 100, new arrays need to be created, and copied which is not easy.



    You should use a collection which has no fixed size: Any one from java.utils will do, like ArrayList, LinkedList, Stack, ...



    LinkedList<Student> students = new LinkedList<Student>();





    share|improve this answer




























      1














      You need to use a collection to store your students! The most simple is an array:



      Student students = new Student[100];


      This will create an array for maximum 100 Students. One problem with this is that it cannot be resized, so if you add more then 100, new arrays need to be created, and copied which is not easy.



      You should use a collection which has no fixed size: Any one from java.utils will do, like ArrayList, LinkedList, Stack, ...



      LinkedList<Student> students = new LinkedList<Student>();





      share|improve this answer


























        1












        1








        1







        You need to use a collection to store your students! The most simple is an array:



        Student students = new Student[100];


        This will create an array for maximum 100 Students. One problem with this is that it cannot be resized, so if you add more then 100, new arrays need to be created, and copied which is not easy.



        You should use a collection which has no fixed size: Any one from java.utils will do, like ArrayList, LinkedList, Stack, ...



        LinkedList<Student> students = new LinkedList<Student>();





        share|improve this answer













        You need to use a collection to store your students! The most simple is an array:



        Student students = new Student[100];


        This will create an array for maximum 100 Students. One problem with this is that it cannot be resized, so if you add more then 100, new arrays need to be created, and copied which is not easy.



        You should use a collection which has no fixed size: Any one from java.utils will do, like ArrayList, LinkedList, Stack, ...



        LinkedList<Student> students = new LinkedList<Student>();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 20:35









        GtomikaGtomika

        406312




        406312

























            0














            You will need to create an array list of Student objects. You could add an unlimited number of Student objects to the array list.



            I played around with it a bit, and I figured out how to add objects to the ArrayList. Here is an example of how you could do this:



            import java.io.BufferedReader;
            import java.io.IOException;
            import java.io.InputStreamReader;
            import java.util.ArrayList;

            public class Students {

            ArrayList<Student> unique = new ArrayList<Student>();

            public void createStudent() throws IOException {

            String firstName = "";
            String lastName = "";
            Student temp;

            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);

            System.out.println("whats your Lastname?");
            lastName = br.readLine();

            System.out.println("and your Firstname?");
            firstName = br.readLine();

            // Create Object with given attributes
            temp = new Student(firstName, lastName);
            unique.add(temp);
            }

            public String getFirstName(int index) {
            return unique.get(index).firstName;
            }

            public String getLastName(int index) {
            return unique.get(index).lastName;
            }

            public static void main(String args) throws IOException {

            Students students = new Students();
            students.createStudent();

            System.out.println(students.getFirstName(0));
            System.out.println(students.getLastName(0));
            }
            }

            class Student {

            String firstName = "";
            String lastName = "";

            public Student(String fn, String ln) {
            firstName = fn;
            lastName = ln;
            }
            }


            Let me know if you need any more help.






            share|improve this answer


























            • And how do I use this ? I mean when I have this line of code, how do I assign the attributes to the Object ?

              – Hecko2g
              Nov 21 '18 at 20:41
















            0














            You will need to create an array list of Student objects. You could add an unlimited number of Student objects to the array list.



            I played around with it a bit, and I figured out how to add objects to the ArrayList. Here is an example of how you could do this:



            import java.io.BufferedReader;
            import java.io.IOException;
            import java.io.InputStreamReader;
            import java.util.ArrayList;

            public class Students {

            ArrayList<Student> unique = new ArrayList<Student>();

            public void createStudent() throws IOException {

            String firstName = "";
            String lastName = "";
            Student temp;

            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);

            System.out.println("whats your Lastname?");
            lastName = br.readLine();

            System.out.println("and your Firstname?");
            firstName = br.readLine();

            // Create Object with given attributes
            temp = new Student(firstName, lastName);
            unique.add(temp);
            }

            public String getFirstName(int index) {
            return unique.get(index).firstName;
            }

            public String getLastName(int index) {
            return unique.get(index).lastName;
            }

            public static void main(String args) throws IOException {

            Students students = new Students();
            students.createStudent();

            System.out.println(students.getFirstName(0));
            System.out.println(students.getLastName(0));
            }
            }

            class Student {

            String firstName = "";
            String lastName = "";

            public Student(String fn, String ln) {
            firstName = fn;
            lastName = ln;
            }
            }


            Let me know if you need any more help.






            share|improve this answer


























            • And how do I use this ? I mean when I have this line of code, how do I assign the attributes to the Object ?

              – Hecko2g
              Nov 21 '18 at 20:41














            0












            0








            0







            You will need to create an array list of Student objects. You could add an unlimited number of Student objects to the array list.



            I played around with it a bit, and I figured out how to add objects to the ArrayList. Here is an example of how you could do this:



            import java.io.BufferedReader;
            import java.io.IOException;
            import java.io.InputStreamReader;
            import java.util.ArrayList;

            public class Students {

            ArrayList<Student> unique = new ArrayList<Student>();

            public void createStudent() throws IOException {

            String firstName = "";
            String lastName = "";
            Student temp;

            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);

            System.out.println("whats your Lastname?");
            lastName = br.readLine();

            System.out.println("and your Firstname?");
            firstName = br.readLine();

            // Create Object with given attributes
            temp = new Student(firstName, lastName);
            unique.add(temp);
            }

            public String getFirstName(int index) {
            return unique.get(index).firstName;
            }

            public String getLastName(int index) {
            return unique.get(index).lastName;
            }

            public static void main(String args) throws IOException {

            Students students = new Students();
            students.createStudent();

            System.out.println(students.getFirstName(0));
            System.out.println(students.getLastName(0));
            }
            }

            class Student {

            String firstName = "";
            String lastName = "";

            public Student(String fn, String ln) {
            firstName = fn;
            lastName = ln;
            }
            }


            Let me know if you need any more help.






            share|improve this answer















            You will need to create an array list of Student objects. You could add an unlimited number of Student objects to the array list.



            I played around with it a bit, and I figured out how to add objects to the ArrayList. Here is an example of how you could do this:



            import java.io.BufferedReader;
            import java.io.IOException;
            import java.io.InputStreamReader;
            import java.util.ArrayList;

            public class Students {

            ArrayList<Student> unique = new ArrayList<Student>();

            public void createStudent() throws IOException {

            String firstName = "";
            String lastName = "";
            Student temp;

            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);

            System.out.println("whats your Lastname?");
            lastName = br.readLine();

            System.out.println("and your Firstname?");
            firstName = br.readLine();

            // Create Object with given attributes
            temp = new Student(firstName, lastName);
            unique.add(temp);
            }

            public String getFirstName(int index) {
            return unique.get(index).firstName;
            }

            public String getLastName(int index) {
            return unique.get(index).lastName;
            }

            public static void main(String args) throws IOException {

            Students students = new Students();
            students.createStudent();

            System.out.println(students.getFirstName(0));
            System.out.println(students.getLastName(0));
            }
            }

            class Student {

            String firstName = "";
            String lastName = "";

            public Student(String fn, String ln) {
            firstName = fn;
            lastName = ln;
            }
            }


            Let me know if you need any more help.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 21:27

























            answered Nov 21 '18 at 20:27









            WeseltonDWWeseltonDW

            134




            134













            • And how do I use this ? I mean when I have this line of code, how do I assign the attributes to the Object ?

              – Hecko2g
              Nov 21 '18 at 20:41



















            • And how do I use this ? I mean when I have this line of code, how do I assign the attributes to the Object ?

              – Hecko2g
              Nov 21 '18 at 20:41

















            And how do I use this ? I mean when I have this line of code, how do I assign the attributes to the Object ?

            – Hecko2g
            Nov 21 '18 at 20:41





            And how do I use this ? I mean when I have this line of code, how do I assign the attributes to the Object ?

            – Hecko2g
            Nov 21 '18 at 20:41


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419781%2fcreating-objects-while-program-runs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)