Results 1 to 13 of 13
- 04-13-2008, 06:08 AM #1
[SOLVED] How do I combine two variables?
I tried looking this up online, but I don't know what to call it...
I'm sure it is a common problem...
for(int i = 0; i <= numStud; i++)
{
Student stud_i = new Student(numCour);
}
Every time the loop restarts, the "i" in "stud_i", should be replaced with the number it represents....
I don't think I can explain it better than that, so I hope that is good enough. lol
Thanks!
- 04-13-2008, 05:31 PM #2
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
I don't think you can make "i" in stud_i increment since java sees it as a variable name. (java only sees one name that's "stud_i"). It doesn't know that "i" is suppose to be increment. Try to use and array. where stud_i become stud[i]. Of course you will need to change some things around, but yeah.
If not an array just use i instead of stud_i.
- 04-13-2008, 05:55 PM #3
I can't just use "i", because "i" is already defined. This is an error.
I don't know what else to do, I can't use a predefined variable.....
I can't have another array with out some hellish rewrites...
Any other Ideas...-- www.firemelt.net --
Cheer up, the worst has yet to come...
- 04-14-2008, 10:08 AM #4
Best way to do what you're trying to accomplish, known as dynamic variable naming, is to use HashMaps.
Java Code:HashMap<String, Student> map = new HashMap<Student> (); for (int i = 0; i < numStud; i++) { map.put("myObj" + i, new Student()); }Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 04-14-2008, 03:51 PM #5
- 04-14-2008, 04:09 PM #6
Please mark this thread as Solved bobleny. Check my sig if you need help.
Did this post help you? Please
me! :cool:
- 04-14-2008, 06:19 PM #7
Yeah, I was looking for a solved button and I didn't find one, so I thought you didn't have one...
OK, all betters.
Thanks again!-- www.firemelt.net --
Cheer up, the worst has yet to come...
- 07-08-2008, 11:58 AM #8
Member
- Join Date
- Jul 2008
- Location
- kolkata
- Posts
- 5
- Rep Power
- 0
Captain , i am getting the error in the first line , ; expected
HashMap(String, Student)map = new HashMap Student();
for (int i = 0; i < 20; i++) {
map.put("myObj" + i, new Student());
}
i'm using this codeLast edited by innocent.crook; 07-08-2008 at 12:05 PM.
- 07-08-2008, 12:22 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
You use greaterThan lessThan signs around those Generics items, not parens.
i.e.
notJava Code:HashMap<String, Student>
and it needs to also appear exactly that way on the right hand side of the equals sign.Java Code:HashMap(String, Student)
- 07-08-2008, 01:08 PM #10
Member
- Join Date
- Jul 2008
- Location
- kolkata
- Posts
- 5
- Rep Power
- 0
m using netbeans..it is showing error "cannot find symbol" for using <>
- 07-08-2008, 01:27 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Probably because you have no "Student" class, which is something that the poster of this question had made himself.
Do you have any idea what a hashmap is? Do you have any idea what Generics is? I hope you can answer yes to both of those questions, since they are to be used in your current homework assignment (help needed regarding LOGIN form), so you have already covered them in your class. Where you paying attention?
- 07-08-2008, 02:01 PM #12
Member
- Join Date
- Jul 2008
- Location
- kolkata
- Posts
- 5
- Rep Power
- 0
i have added key value pairs using hashMap ..n m able to print the keys for the corresponding values.
i want to make the key unique...if a same key is added it will return a boolean false n say ..can't add. and can we add key n value through keyboard??
- 07-08-2008, 03:15 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Use a Set or the containsKey method
Attempt to add it to the Set first, or call the containsKey method....if a same key is added it will return a boolean false n say
If you write the user interaction, of course, intrinsicaly, no...can't add. and can we add key n value through keyboard??
Similar Threads
-
Combine package of(jdk,eclipse and tomcat)
By joseph in forum EclipseReplies: 0Last Post: 04-07-2008, 01:18 PM -
Initialize variables before use
By Java Tip in forum Java TipReplies: 0Last Post: 12-22-2007, 11:22 AM -
Variables
By mew in forum New To JavaReplies: 3Last Post: 12-11-2007, 12:44 PM -
JSP - session variables
By Java Tip in forum Java TipReplies: 0Last Post: 12-02-2007, 09:22 PM -
Help with variables in java
By fernando in forum New To JavaReplies: 2Last Post: 08-06-2007, 05:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks