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 2 of 2

Thread: using input from a user as an object reference variable

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using input from a user as an object reference variable

    Hello all -- new here; first post. Not sure if this is the right place, so my apologies.

    I'm sort of a beginner; going through "Head First Java," and was wondering if it's possible to take input from a user and use that as a reference variable for an object. I want to try and make something like a contacts application for myself, as a way of learning the language, and currently have a method where the user enters a string, and I tried to use that string as the name for the new object.

    Turns out that I can't use the string as the reference variable; got an error message.

    I want to create the object (as I said, using the entered information from the suer as the reference variable), then fill in all the instance variables, but I'm not sure how to do that, if it can be done at all. Is there anyone out there who can offer some help?

    Again, my apologies if this is the wrong forum for this question.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using input from a user as an object reference variable

    What have you tried? Please post the code you are working with wrapped in code tags.
    Also post any error messages you get when compiling the code.

    take input from a user and use that as a reference variable for an object.
    A Map<String, MyObject> instance would allow you to take a String from a user and use that as a key to a Map to get a reference to a MyObject instance.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: April 19th, 2014, 03:47 PM
  2. Permanently change variable values based on user input.
    By joseph.clevenger7 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 26th, 2013, 07:32 PM
  3. How can I have the user input the name of an object?
    By kkid in forum Object Oriented Programming
    Replies: 2
    Last Post: November 12th, 2012, 06:03 PM
  4. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  5. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM