Results 1 to 8 of 8
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    236
    Rep Power
    3

    Default Have user input custom variable name

    How can I have a user input a variable name?
    I know for ints this can be used:

    Java Code:
    Scanner userInput = new Scanner(System.in);
            int userSelection = userInput.nextInt();

    but is there anything which would allow, instead of ints, the user to input a variable of one of my custom classes?
    e.g. I have an object called AtoCSeat, can I have the user input an AtoC variable name and have it assigned to another variable?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Have user input custom variable name

    You don't want to do this, trust me. Variable names are not nearly as important as many think they may be, and in fact, they really don't exist (for the most part) during run-time. Much more important are object references, and I think that instead you'll want to concentrate on that. For instance, if you want to associate an int with a String, consider using a Map<String, Integer> for that so that you can get a reference to the int (or here its object wrapper, Integer) with your key String.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Have user input custom variable name

    Quote Originally Posted by Jdsfighter View Post
    Perhaps

    Java Code:
    (Object) userSelection
    Haven't tried it, but its worth a shot.
    I think that you'll want to delete this suggestion as I strongly believe that it is misleading.

  4. #4
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    236
    Rep Power
    3

    Default Re: Have user input custom variable name

    Quote Originally Posted by Fubarable View Post
    You don't want to do this, trust me. Variable names are not nearly as important as many think they may be, and in fact, they really don't exist (for the most part) during run-time. Much more important are object references, and I think that instead you'll want to concentrate on that. For instance, if you want to associate an int with a String, consider using a Map<String, Integer> for that so that you can get a reference to the int (or here its object wrapper, Integer) with your key String.

    I am very new to Java and haven't done anything to do with mappings yet.

    Because I am making a seat bookings system, I need the user to enter the seat name (which refers to an object) then all I will use this name for is to call a variable value (e.g A2.isBooked). Do I need mappings for this?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Have user input custom variable name

    I wouldn't use Strings or names (or variable "names" at all). I'd create a Passenger object with String for name and any other Passenger-y information, I'd create a bunch of other classes including FlightTicket class a Flight class, a SeatAssignment class,...

    And I'd create an ArrayList of Passengers that the Flight object would hold. Either that or you could use a Map such as a HashMap, but I'd use SeatAssignment as my Map's key and Passenger as its value.

    But again, most definitely and most emphatically, whatever you do, you do not want to do this with variable "names". Trust me, that's not the zen of Java.

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    236
    Rep Power
    3

    Default Re: Have user input custom variable name

    To be honest, this is all sounding quite complex now.

    I attempted something similar to what you said in your first post:
    For instance, if you want to associate an int with a String, consider using a Map<String, Integer> for that so that you can get a reference to the int (or here its object wrapper, Integer) with your key String.

    By writing this (quickly, untested), after doing lots of research on HashMaps:
    Java Code:
    public static void seatMapping(){
            HashMap<String, AtoCSeat> seatMap = new HashMap<String, AtoCSeat>();
            
            seatMap.put("A1", A[1]);
        }

    My theory was that if the user entered A1, it could get mapped to the A[1] AtoCSeat object (as an example).
    However, you said about not using variable names.
    Would, in theory, this way work? I am not too bothered if it is a bad way to do it, I just need it to work from the users perspective at the moment - the reason for this is that this program I am writing is just a basic prototype for functionality at the moment fro me to show some people later this week which I will improve on when I re-make it soon.

  7. #7
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    236
    Rep Power
    3

    Default Re: Have user input custom variable name

    I have thought of a workaround, I know it is crude and lengthy but would this still work?

    Java Code:
    public static AtoCSeat seatWorkaround(String userSelection){
            
            if (userSelection.equals("A1")){
                return A[1];
            
        }
            // Followed by other if statements after...
        }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Have user input custom variable name

    Consider showing us more as I'm not sure of your context here, for instance just what is held in your A[1] array -- I guess an AtoCSeat whatever that is, but again I'm not sure. Also, you will want to avoid using static methods or variables. Also, your variable and method names should start with a lower case letter not capital letter.

Similar Threads

  1. user input on gui
    By JoePenguin in forum New To Java
    Replies: 5
    Last Post: 01-26-2012, 07:27 PM
  2. Direction for creating a custom JTextField input
    By sp33dy in forum AWT / Swing
    Replies: 4
    Last Post: 08-17-2011, 09:08 PM
  3. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  4. how to use user name as a session variable in jsp
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 05-05-2010, 09:09 AM
  5. How to create custom Input Dialog
    By Bab in forum SWT / JFace
    Replies: 2
    Last Post: 08-02-2009, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •