Results 1 to 4 of 4
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Rep Power

    Red face Class name generator

    I'm a newbie to java, and at the moment I'm just playing around with it.

    I'm trying to make a program that will let the user input a value (an int), which will be set as the variable in a class.

    UserInput = the value of a variable in a class

    Now, what I want to do is create my program in such a way that it will create a new instance of that class for each time the user puts in an int.


    user inputs the numbers: 1,5,8,9
    results in:

    class1 with variable value 1
    class2 with variable value 5
    class3 with variable value 8
    class4 with variable value 9


    In order to do this I have attempted to create a while loop:

    while userinput != 0
    Class class1 = new class ();
    PROMPT USER for next integer.

    The obvious problem with the while loop above (besides it not being written properly, it is merely a demonstration) is that the variable will change only in class1 or maybe create an error when trying to create a new instance of a class with the same name.

    My question is:

    How can I write the while loop in such a way that the class name will change (for example from class1 to class2) automatically when the user enters another integer?


    What I have tried (but probably not done correctly) is to create a variable className and then have the while loop look like this:

    Class class+className = new class ();
    className = className +1;

    This didn't work and I'm guessing its for obvious reasons though I don't see them (too much of a newbie) :(

    So how can I approach this issue?

    Thanks in advance. :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    First of all, you really can't do what you're trying to do, but more importantly, you don't want to. The variable name is not as important as you think it is and for all intents and purposes doesn't really exist when the program is running. What is more important is having references to objects, not having names for object variables. So if you want to create a bunch of MyClass objects, each with a different int property, I recommend creating an ArrayList of MyClass, and then filling it with MyClass objects as you create them, something like:

    Java Code:
    // note code not compiled, not tested
    Scanner scanner = new Scanner(;
    ArrayList<MyClass> myClassList = new ArrayList<MyClass>();
    int input = 1;
    do  {
      System.out.print("Enter an int: ");
      input = scanner.nextInt();
      MyClass myClass = new MyClass(input);
    } while (input != sentinalValue);
    corrected so that input changes
    Last edited by Fubarable; 04-09-2011 at 06:28 PM. Reason: corrected

  3. #3
    Vase is offline Member
    Join Date
    Jan 2011
    Gainesville, FL
    Rep Power


    You are looking for arrays, or an ArrayList object if you don't have a specific number of inputs.

    If using arrays:

    Java Code:
    int numberOfInstances;
    Class[] class = new Class[10]; // initialized to a maximum of 10 inputs
    while (userInput != 0) {
       class[numberOfInstances] = new Class();

  4. #4
    aianta is offline Member
    Join Date
    Apr 2011
    Rep Power


    Thanks, for the quick reply, I think I see what you mean, gonna try it out :)

Similar Threads

  1. Replies: 0
    Last Post: 01-25-2011, 07:02 AM
  2. Random Generator
    By Learning Java in forum New To Java
    Replies: 9
    Last Post: 05-31-2010, 08:15 PM
  3. UML class diagram generator!
    By renegadeandy in forum Advanced Java
    Replies: 5
    Last Post: 04-11-2009, 07:08 AM
  4. Help with class project, random number generator.
    By Christopher The Great in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 03:12 AM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 07:43 PM

Tags for this Thread

Posting Permissions

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