Results 1 to 5 of 5
  1. #1
    azaleah is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default String array to position integers

    Hi there,

    I have an array of Strings. For example String[] ary = {"bob", "mary", "ken"}; .
    I'd like to write code that -for each String in the String array- makes an integer which is named after the name in that position and gets it's value assigned to the String's position in the String element.
    For example: I want to make integers bob=1, mary=2, ken=3.

    my code so far is:
    for(int i=0 ; i < ary.length ; i++) int ary[i]=i;

    which doesn't work of course... help please!~

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: String array to position integers

    You could make a parallel int array,

    Java Code:
    int[] intArray = new int[ary.length];
    for (int i = 0; i < intArray.length; i++) {
       intArray[i] = i;
    }
    (note that arrays are 0 based, and so this will associate 0 with the first String, not 1).
    But this is not an elegant solution as it requires one to always be sure to modify the one array if one modifies the other -- a recipe for future disaster.

    Another possible solution is to us a Map<String, Integer> such as that provided by a HashMap<String, Integer> and put the desired int with the String.
    Java Code:
    Map<String, Integer> myMap = new HashMap<String, Integer>();
    for (int i = 0; i < ary.length; i++) {
       myMap.put(ary[i], (i + 1));
    }
    Another possible solution is to create a class that holds an int and a String, and then create an array of objects of this class rather than one String array and the Map.

  3. #3
    azaleah is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: String array to position integers

    thanks a lot!

  4. #4
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: String array to position integers

    Did you want to generate some MACRO code like SAS,C?

  5. #5
    azaleah is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: String array to position integers

    The mapping has worked well for my purposes but thanks for offering your help :)

Similar Threads

  1. how do you add up integers in an array?
    By shazakala in forum New To Java
    Replies: 7
    Last Post: 04-19-2011, 10:32 AM
  2. getting integers from a string
    By sehudson in forum New To Java
    Replies: 22
    Last Post: 02-18-2011, 05:34 AM
  3. Multiples Integers from a String
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 04-01-2009, 12:00 AM
  4. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  5. how to get the Integers out of a String
    By JordashTalon in forum New To Java
    Replies: 10
    Last Post: 01-30-2009, 06:28 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
  •