Results 1 to 13 of 13
  1. #1
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default [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!

  2. #2
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    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.

  3. #3
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

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

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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)

  5. #5
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

    That will work...

    Thanks Captain!
    -- www.firemelt.net --
    Cheer up, the worst has yet to come...

  6. #6
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Please mark this thread as Solved bobleny. Check my sig if you need help.
    Did this post help you? Please me! :cool:

  7. #7
    bobleny's Avatar
    bobleny is offline Member
    Join Date
    Apr 2008
    Posts
    50
    Rep Power
    0

    Default

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

  8. #8
    Join Date
    Jul 2008
    Location
    kolkata
    Posts
    5
    Rep Power
    0

    Default

    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 01:05 PM.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You use greaterThan lessThan signs around those Generics items, not parens.

    i.e.
    Java Code:
    HashMap<String, Student>
    not
    Java Code:
    HashMap(String, Student)
    and it needs to also appear exactly that way on the right hand side of the equals sign.

  10. #10
    Join Date
    Jul 2008
    Location
    kolkata
    Posts
    5
    Rep Power
    0

    Default

    m using netbeans..it is showing error "cannot find symbol" for using <>

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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?

  12. #12
    Join Date
    Jul 2008
    Location
    kolkata
    Posts
    5
    Rep Power
    0

    Default

    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??

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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

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

Similar Threads

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

Posting Permissions

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