Results 1 to 4 of 4
Thread: Class name generator
- 04-09-2011, 06:16 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Class name generator
Hi,
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.
so:
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.
Ex:
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 ();
class.set_value(userinput)
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. :)
-
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:
corrected so that input changesJava Code:// note code not compiled, not tested Scanner scanner = new Scanner(System.in); ArrayList<MyClass> myClassList = new ArrayList<MyClass>(); int input = 1; do { System.out.print("Enter an int: "); input = scanner.nextInt(); MyClass myClass = new MyClass(input); myClassList.add(myClass); } while (input != sentinalValue);Last edited by Fubarable; 04-09-2011 at 06:28 PM. Reason: corrected
- 04-09-2011, 06:22 PM #3
Member
- Join Date
- Jan 2011
- Location
- Gainesville, FL
- Posts
- 45
- Rep Power
- 0
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(); class[numberOfInstances].set_value(userInput); numberOfInstances++; }
- 04-09-2011, 06:25 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 60
- Rep Power
- 0
Similar Threads
-
<generator class = 'select'>, no natural-id property defined; need to specify [key]
By ash_gj1 in forum Advanced JavaReplies: 0Last Post: 01-25-2011, 06:02 AM -
Random Generator
By Learning Java in forum New To JavaReplies: 9Last Post: 05-31-2010, 08:15 PM -
UML class diagram generator!
By renegadeandy in forum Advanced JavaReplies: 5Last Post: 04-11-2009, 07:08 AM -
Help with class project, random number generator.
By Christopher The Great in forum New To JavaReplies: 4Last Post: 03-14-2009, 02:12 AM -
Random Generator
By padutch2 in forum New To JavaReplies: 1Last Post: 12-03-2007, 06:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks