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.


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

Results 1 to 2 of 2

Thread: Possible to dynamically create strings?

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Thanked 0 Times in 0 Posts

    Question Possible to dynamically create strings?

    I have recently got back in to Java after a number of years so I am slowly getting back up to speed again. I was wondering if anybody can help or point me in the right direction with the following:

    I have written a UI which depending on the options chosen generates a properties file which I am then using in a method.
    Certain parts of the properties file are always the same and I have successfully written code to read from the file, parse the information and add it to a string.
    My problem is that depending on the options from the UI there can be X number of lines that I want to read into strings, for example part of the properties file looks like this:
    xA1=Answer To XQ1
    xA2=Answer To XQ2
    xA3=Answer To XQ3
    What I am stuck with is a way in my java method to dynamically create strings to hold this information.

    Can anyone suggest a way to do this or recommend something else to archive this?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,702 Times in 2,652 Posts

    Default Re: Possible to dynamically create strings?

    there can be X number of lines that I want to read into strings
    That sounds like you need to use a collection like an ArrayList to contain the Strings. It can hold X number of items.
    method to dynamically create strings to hold this information.
    Not sure what you mean by "dynamically create strings". You seem to be asking how to create new variable names when a program executes. Variable names are created when a file is editted. Using a collection will give you a "new name" for a variable that includes the collection name plus the value of the index into the collection: collection.get(0) is the "name" for the first item in the collection,
    collection.get(1) is the name for the second item, etc

    Instead of having parallel collections, create an object that contains the question and its answer and put those new objects into the collection.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to create arrays dynamically??
    By shubhen in forum Java SE APIs
    Replies: 1
    Last Post: July 27th, 2011, 07:24 AM
  2. Question Dynamically create objects, set value and call
    By buntyindia in forum Java Theory & Questions
    Replies: 1
    Last Post: May 25th, 2011, 07:50 AM
  3. Adding strings to an array dynamically, how can i do it?
    By emkoirl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2011, 02:07 PM
  4. Create a tree from list of strings
    By ayri in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2010, 04:32 AM
  5. [SOLVED] How to dynamically create ArrayLists (or something similar)?
    By igniteflow in forum Collections and Generics
    Replies: 4
    Last Post: August 6th, 2009, 06:00 AM

Tags for this Thread