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

    Default Converting String Array To Map - Beginner Help

    Hello. I am just starting to learn Java in order to help a friend, and I have a quick question.

    I am wondering how I can convert a string array to a map (or if this is possible at all). My array elements are in the format "key:value", which is fine but I need to be able to access it via the "key" instead of using the index number. I have read that maps allow key/value pairs, so I am curious if I can convert my array into a map with ":" as the separator of the keys and values. For instance, this is what the oringal array looks like (not actual code, just an example):

    Original Array (or something like this): { SEVEN:0.00013, ALPHA:TWO, LARRY:2LE }

    The array is currently accessed this way: temp = Comm.breakStringGetLast(commandParameters[1], ":").trim();

    Converted Map (or soemthing like this; not sure if this is how a map looks but you get the idea): { { "SEVEN", "0.00013" }, { "ALPHA", "TWO" }, { "LARRY", "2LE" } }

    I am needing to be able to access each value (the part after the ":", such as "0.00013") by using the key (the part before the ":", such as "ALPHA") so that the order of the elements no longer matters. I don't know if a map is the best way to do this, but I am curious to see if/how this works. Does anyone know how I can do this? Thank you for the help, and have a great day.


    Brendan "NinjaNife" West
    Last edited by NinjaNife; 02-06-2012 at 03:16 PM.

  2. #2
    ContoNick is offline Member
    Join Date
    Dec 2011
    Location
    Croatia,Zagreb
    Posts
    24
    Rep Power
    0

    Default Re: Converting String Array To Map - Beginner Help

    Hi.
    Im beginner like u.
    I would do this like this, inserting "key:value" in map:

    Java Code:
    public class Test {
    
    	static String[] array =  { "SEVEN:0.00013", "ALPHA:TWO", "LARRY:2LE" };
    	
    	
    	public static HashMap<String, String> convertToMap(String [] someArray){
    		HashMap<String,String> retMap = new HashMap<String, String>();
    		
    		for (String string : array) {
    			String splitFirstValue = string.substring(0,string.indexOf(":"));
    			String splitSeconSValue = string.substring(string.indexOf(":") + 1 ,string.length());
    			
    			retMap.put(splitFirstValue, splitSeconSValue);
    		}
    		return retMap;
    	}
    	
    	
    	public static void main(String[] args) throws Exception {
    		HashMap<String,String> someMap = convertToMap(array);
    		System.out.println();
    		
    	}
    
    }
    hope this help u.
    If u got any problem, feel free write here :)

    For access to the value's in map read this http://docs.oracle.com/javase/1.4.2/.../util/Map.html
    Last edited by ContoNick; 02-06-2012 at 03:54 PM.

  3. #3
    NinjaNife is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Converting String Array To Map - Beginner Help

    Quote Originally Posted by ContoNick View Post
    Hi.
    Im beginner like u.
    I would do this like this, inserting "key:value" in map:

    Java Code:
    public class Test {
    
    	static String[] array =  { "SEVEN:0.00013", "ALPHA:TWO", "LARRY:2LE" };
    	
    	
    	public static HashMap<String, String> convertToMap(String [] someArray){
    		HashMap<String,String> retMap = new HashMap<String, String>();
    		
    		for (String string : array) {
    			String splitFirstValue = string.substring(0,string.indexOf(":"));
    			String splitSeconSValue = string.substring(string.indexOf(":") + 1 ,string.length());
    			
    			retMap.put(splitFirstValue, splitSeconSValue);
    		}
    		return retMap;
    	}
    	
    	
    	public static void main(String[] args) throws Exception {
    		HashMap<String,String> someMap = convertToMap(array);
    		System.out.println();
    		
    	}
    
    }
    hope this help u.
    If u got any problem, feel free write here :)
    Thanks! I do have one more request; could you please break that down and explain it to me so I understand how each part works? I am *very* new to Java, so although I know that may work, I would like to understand what each part does/how it does it (there is a lot of stuff there that I have not seen before). Thanks for the help!


    Brendan "NinjaNife" West

  4. #4
    ContoNick is offline Member
    Join Date
    Dec 2011
    Location
    Croatia,Zagreb
    Posts
    24
    Rep Power
    0

    Default Re: Converting String Array To Map - Beginner Help

    Ok. ill try explaint it but my english in not perfect :)

    Java Code:
    static String[] array =  { "SEVEN:0.00013", "ALPHA:TWO", "LARRY:2LE" };
    your String array obviusly :D

    Java Code:
    public static HashMap<String, String> convertToMap(String [] someArray){
    this is method which accept's your string array and return's HashMap<String, String>

    Java Code:
    HashMap<String,String> retMap = new HashMap<String, String>();
    creating object of class HashMap called retMap where the KEY is object of String and VALUE is object of String

    Now u have for loop which loop's thru every String object in String[] array

    Java Code:
    String splitFirstValue = string.substring(0,string.indexOf(":"));
    string.indexOf() return's the number(INT) where he found first char ":"
    string.substring() return's substring from position 0 to the position where first ":" was found

    Java Code:
    String splitSeconSValue = string.substring(string.indexOf(":") + 1 ,string.length());
    same as above,
    + 1 <- becouse u get position where ":" is, and u need jump over it on next character, string.length() selfexplain's :D

    Java Code:
    retMap.put(splitFirstValue, splitSeconSValue);
    put method's insert's/put's elemnt's in map.
    for your first String KEY is SEVEN and VALUE 0.00013

    Java Code:
    return retMap;
    method return's just created map

    Java Code:
    public static void main(String[] args) throws Exception {
    		HashMap<String,String> someMap = convertToMap(array);
    		
    		System.out.println();
    		
    	}
    here u run your program, create object someMap, call method convertToMap and pass as argument your array which u want to convert.

    Java Code:
    System.out.println();
    totatly useless now, this print's one line in console, but i used it as breakpoint place here

    example how it look's in map / eclipse debugger screen in attachment :)
    Attached Thumbnails Attached Thumbnails Converting String Array To Map - Beginner Help-ojlbw9.jpg  

  5. #5
    NinjaNife is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Converting String Array To Map - Beginner Help

    Thanks! This is very insightful :D I will take a look at this and see if I can integrate it into my friend's code. If I have any more questions I will be sure to ask. Thanks again for your help :)

    Brendan "NinjaNife" West

Similar Threads

  1. CONVERTING BYTE ARRAY to STRING
    By Sukeshps in forum New To Java
    Replies: 5
    Last Post: 01-18-2012, 06:08 PM
  2. Converting Arraylist to String array
    By ankit1801 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 10:07 AM
  3. Replies: 6
    Last Post: 02-17-2011, 01:22 PM
  4. Converting to String
    By darek9576 in forum New To Java
    Replies: 1
    Last Post: 03-13-2010, 10:07 PM
  5. Converting a String into a array.
    By taraxgoesxboom in forum New To Java
    Replies: 12
    Last Post: 02-22-2009, 05:29 AM

Posting Permissions

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