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

Thread: How can I have the user input the name of an object?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I have the user input the name of an object?

    I am making a theatre bookings system for some school work.

    I have variables A1, A2, A3, A4 etc... all of type AtoCSeat (Which I have created).

    I want the user to be able to remove a booking, however I cannot figure out how to have the user input the desired seat due to the variable not being a String and being of type AtoCSeat and therefore the Scanner not accepting it.


    I know this code won't work, but I was thinking something along the lines of:

    System.out.println("Which seat would you like to remove a booking for?");
            Scanner userInput = new Scanner(System.in);
            AtoCSeat userSelection = userInput.nextLine();       // won't work as nextLine() looks for Strings


    Can anybody help me please?
    Once I know how to get java to understand the user input of a seat, I can actually finish the rest of my program myself, it is just this...


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,217
    Thanks
    175
    Thanked 815 Times in 758 Posts
    Blog Entries
    5

    Default Re: How can I have the user input the name of an object?

    AtoCSeat userSelection = userInput.nextLine();       // won't work as nextLine() looks for Strings

    The above will not work because the nextLine() method of scanner returns a String. If you wish to have AtoCSeat, you need a way to convert the returned String to an instance of this class. There are quite possible dozens of ways to do this. You could do so be writing an appropriate constructor, or if you have already created the instances somewhere else in your program, then you could initially map them using a Map keyed with the String value (see The Map Interface (The Java™ Tutorials > Collections > Interfaces) ) and retrieve the appropriate instance this way.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How can I have the user input the name of an object?

    This has already been discussed with you on a different forum and you've been told that your original idea won't work for Java, that variable names are not what you need to concentrate on, that you need to deal with references including use of collections and possible use of Maps. Why are you ignoring previous advice and asking this question again, potentially wasting folks (including Copeg's) time? I think most would appreciate it if you at least provided links to your previous similar post so folks know what has already been discussed.

Similar Threads

  1. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  2. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 07:14 PM
  3. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  4. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 10:23 AM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM