Results 1 to 8 of 8
  1. #1
    dr_duckrat is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Atomatically make new variables with function?

    First off, I'm not entirely sure if this is the right place to post this, but I'm trying to make a method that creates and names variables based on how many times it was run. For example, if it was run once, it would make a variable called newVariable1. The second time it would make newVariable2, or something like that. The point is that the method needs to name the variables automatically without me manually declaring the variable. Is this even possible, and if so, how would I go about doing it? Thanks if advance to all you wonderful people

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Atomatically make new variables with function?

    Quote Originally Posted by dr_duckrat View Post
    but I'm trying to make a method that creates and names variables based on how many times it was run.
    By how many times what was run? I am unaware of any way to add symbols to the JRE symbol table. But if you want to "create" variables you could use a Map to index into a table. I believe you need to provide more info.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    dr_duckrat is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: Atomatically make new variables with function?

    By how many times what was run?
    What I'm trying to do is make a calculator that reads a string that contains an entire equation (like 12*2+5), then separates the numbers from the operators and stores them in their own variables. The idea is that these variables will be created by the program itself without me manually declaring them. For example, If I had 12*2+5 as my equation, the program would store 12 in a variable called strNumber1, then put 2 in strNumber2 and 5 in strNumber3. Then It would put "*" in strOperator1 and "+" in strOperator2.

    Actually, if there is a simpler way to do this, I would like to know. But I would still like to know how I would use my method above, in case I would need to use it in the future.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Atomatically make new variables with function?

    If you want to be able to evaluate expressions the easiest way is to convert the expression into postfix (aka reverse polish notation). Then it becomes very easy to evaluate it using a stack. Just search RPN or postfix notation on the web. Otherwise, you can create a Map<String,Integer> and store the operands there.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    dr_duckrat is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: Atomatically make new variables with function?

    I just realized that the Map solution will probably work! I'm going to write the program right now and see how it turns out. Thanks Jim.

  6. #6
    dr_duckrat is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: Atomatically make new variables with function?

    I'm new to Java and I've never used a Map. I tried to learn, but I can't find anything that's helpful. Can anyone point me to a tutorial online or something?

  7. #7
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    167
    Rep Power
    0

    Default Re: Atomatically make new variables with function?

    I just wrote this exact same program a couple of days ago - I found it was most useful to use an ArrayList

    If I were you, I would just google it and take a look, but here are the basics

    To declare:
    Java Code:
    ArrayList<Integer> name = new ArrayList<Integer>();
    Can hold any data type that you want - just change Integer to String for it to hold Strings, etc, etc

    To add:
    Java Code:
    name.add(5)
    To retrieve:
    Java Code:
    name.get(0)
    The handy thing about them is that they are very easy to change. You can delete and change entries. It was perfect for my calculator, especially since I made it follow order of operations.

    Example:
    Java Code:
    import java.util.ArrayList;
    class Example {
    public static void main (String[] args) {
    ArrayList<String> hold = new ArrayList<String>();
    hold.add("Hey");
    hold.add("my");
    hold.add("name");
    hold.add("is");
    hold.add("Alex.");
    for(int i = 0; i < hold.size(); i++) {
    System.out.print(hold.get(i) + " ");
    }
    }
    }
    Sorry about the lack of indenting.

    That would print out "Hey my name is Alex."

    It very handy for a calculator (at least the way I wrote mine).


    In fact, if you want to see how I did it, I posted a thread on here about it a couple of days ago to see if anybody could tell me a more efficient way to do it:
    Finished Code - More Efficient Way?
    Last edited by AlexGraal; 01-13-2014 at 05:35 AM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Atomatically make new variables with function?

    The Map Interface (The Java™ Tutorials > Collections > Interfaces)

    Basically, they work with key/value pairs

    Java Code:
    Map<String,Integer> ops = new HashMap<>();
    ops.put("MyVal", 10); // associates 10 with the key MyVal
    System.out.println(ops.get("MyVal")); // should print 10.
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 16
    Last Post: 07-05-2013, 09:01 AM
  2. How to make my project function using Netbeans
    By emberato in forum New To Java
    Replies: 1
    Last Post: 10-30-2011, 06:48 PM
  3. How to make a function?
    By mephisto772 in forum New To Java
    Replies: 5
    Last Post: 02-23-2009, 09:51 AM
  4. How to make print function for single label?
    By makpandian in forum AWT / Swing
    Replies: 2
    Last Post: 02-12-2009, 05:26 PM
  5. Replies: 2
    Last Post: 08-03-2007, 04:06 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
  •