|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-13-2008, 08:08 AM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
[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, 07:31 PM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 23
|
|
|
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, 07:55 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
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...
__________________
-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. --
Cheer up, the worst has yet to come...
|
|

04-14-2008, 12:08 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Best way to do what you're trying to accomplish, known as dynamic variable naming, is to use HashMaps.
HashMap<String, Student> map = new HashMap<Student> ();
for (int i = 0; i < numStud; i++) {
map.put("myObj" + i, new Student());
}
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

04-14-2008, 05:51 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
That will work...
Thanks Captain!
__________________
-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. --
Cheer up, the worst has yet to come...
|
|

04-14-2008, 06:09 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 239
|
|
|
Please mark this thread as Solved bobleny. Check my sig if you need help.
__________________
Did this post help you? Please To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. me! To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-14-2008, 08:19 PM
|
 |
Member
|
|
Join Date: Apr 2008
Posts: 36
|
|
|
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!
__________________
-- To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. --
Cheer up, the worst has yet to come...
|
|

07-08-2008, 01:58 PM
|
|
Member
|
|
Join Date: Jul 2008
Location: kolkata
Posts: 5
|
|
|
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 code
Last edited by innocent.crook : 07-08-2008 at 02:05 PM.
|
|

07-08-2008, 02:22 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 429
|
|
You use greaterThan lessThan signs around those Generics items, not parens.
i.e.
not
and it needs to also appear exactly that way on the right hand side of the equals sign.
|
|

07-08-2008, 03:08 PM
|
|
Member
|
|
Join Date: Jul 2008
Location: kolkata
Posts: 5
|
|
|
m using netbeans..it is showing error "cannot find symbol" for using <>
|
|

07-08-2008, 03:27 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 429
|
|
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, 04:01 PM
|
|
Member
|
|
Join Date: Jul 2008
Location: kolkata
Posts: 5
|
|
|
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, 05:15 PM
|
|
Senior Member
|
|
Join Date: Jun 2008
Posts: 429
|
|
Originally Posted by innocent.crook
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
Use a Set or the containsKey method
...if a same key is added it will return a boolean false n say
Attempt to add it to the Set first, or call the containsKey method.
..can't add. and can we add key n value through keyboard??
If you write the user interaction, of course, intrinsicaly, no.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|