Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-13-2008, 08:08 AM
bobleny's Avatar
Member
 
Join Date: Apr 2008
Posts: 36
bobleny is on a distinguished road
[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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-13-2008, 07:31 PM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-13-2008, 07:55 PM
bobleny's Avatar
Member
 
Join Date: Apr 2008
Posts: 36
bobleny is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-14-2008, 12:08 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Best way to do what you're trying to accomplish, known as dynamic variable naming, is to use HashMaps.

Code:
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)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-14-2008, 05:51 PM
bobleny's Avatar
Member
 
Join Date: Apr 2008
Posts: 36
bobleny is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-14-2008, 06:09 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 239
DonCash will become famous soon enoughDonCash will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-14-2008, 08:19 PM
bobleny's Avatar
Member
 
Join Date: Apr 2008
Posts: 36
bobleny is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-08-2008, 01:58 PM
Member
 
Join Date: Jul 2008
Location: kolkata
Posts: 5
innocent.crook is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-08-2008, 02:22 PM
Senior Member
 
Join Date: Jun 2008
Posts: 429
masijade is on a distinguished road
You use greaterThan lessThan signs around those Generics items, not parens.

i.e.
Code:
HashMap<String, Student>
not
Code:
HashMap(String, Student)
and it needs to also appear exactly that way on the right hand side of the equals sign.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-08-2008, 03:08 PM
Member
 
Join Date: Jul 2008
Location: kolkata
Posts: 5
innocent.crook is on a distinguished road
m using netbeans..it is showing error "cannot find symbol" for using <>
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-08-2008, 03:27 PM
Senior Member
 
Join Date: Jun 2008
Posts: 429
masijade is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-08-2008, 04:01 PM
Member
 
Join Date: Jul 2008
Location: kolkata
Posts: 5
innocent.crook is on a distinguished road
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??
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-08-2008, 05:15 PM
Senior Member
 
Join Date: Jun 2008
Posts: 429
masijade is on a distinguished road
Quote:
Originally Posted by innocent.crook View Post
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

Quote:
...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.

Quote:
..can't add. and can we add key n value through keyboard??
If you write the user interaction, of course, intrinsicaly, no.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine package of(jdk,eclipse and tomcat) joseph Eclipse 0 04-07-2008 03:18 PM
Initialize variables before use Java Tip Java Tips 0 12-22-2007 01:22 PM
Variables mew New To Java 3 12-11-2007 02:44 PM
JSP - session variables Java Tip Java Tips 0 12-02-2007 11:22 PM
Help with variables in java fernando New To Java 2 08-06-2007 07:03 PM


All times are GMT +3. The time now is 11:03 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org