Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: loop generate?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default loop generate?

    hi. i have a hashmap like this:
    <key1, <1,3,6>>
    <key2, <2,4,6>>
    <key3, <4,7,9>>

    and i want output like this:

    key1, 1, key2, 2, key3, 4
    key1, 1, key2, 2, key3, 7
    key1, 1, key2, 2, key3, 9
    key1, 1, key2, 4, key3, 4
    key1, 1, key2, 4, key3, 7
    key1, 1, key2, 4, key3, 9
    key1, 1, key2, 6, key3, 4
    key1, 1, key2, 6, key3, 7
    key1, 1, key2, 6, key3, 9

    the problem is: the integer-array is variable as the number of keys also... but i need every combination. how do i do so?


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: loop generate?

    and i want output like this:
    had you tried something to solve this problem.Sorry, we can help you only if you show some effort from your side.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: loop generate?

    Please post something we can compile.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: loop generate?

    hi. here is my code.
    the output is broken because the value is an array... is iterator the right way?! as said in the first post, i need to get every combination of the hash-values
    sample input-text looks like this:

    generic data = 1,2,3
    generic test = a,b
    generic rums = 12,13


    import java.io.BufferedReader; 
    import java.io.FileReader; 
    import java.util.*; 
     
    public class HashMapEx { 
     
        public static void main(String[] args) { 
            HashMap<String, String[]> metadata = new HashMap<String, String[]>();
     
            try { 
                // Opening file 
                // change "/Users/anyexample/input.txt" to path to your test file  
                BufferedReader in = new BufferedReader(new FileReader("test.txt"));
                // string buffer for file reading   
                String str;
     
                // reading line by line from file    
                while ((str = in.readLine()) != null) { 
                    //split string from metafile at "="
     
    				String[] splitarray = str.split("\\=");
    				//remove whitespaces from inputfile...
     
    				splitarray[0] = splitarray[0].trim();
    				splitarray[1] = splitarray[1].trim();
     
    				//test if splitted string starts with "generic"
    				//here also the hashmap is filled with the data from generics!
    				if (splitarray[0].startsWith("generic")){
    					String[] subsplit1 = splitarray[0].split("\\s+");
    					splitarray[0] = subsplit1[1];
     
     
     
    					String[] subsplit2 = splitarray[1].split("\\,");
    					//splitarray[1] = subsplit2;
    					for (int index=0; index<subsplit2.length; index++){
    							System.out.println(splitarray[0] + " " + subsplit2[index]);
    					}
    					// fill our hashmap with the wanted data...
    					metadata.put(splitarray[0], subsplit2);
    				//}
    				}
     
    			}
    			System.out.println("found " + metadata.size() + " generics");
     
    			Set set = metadata.entrySet();
    			Iterator i = set.iterator();
     
    			while(i.hasNext()){
    			      Map.Entry me = (Map.Entry)i.next();
    			      System.out.println(me.getKey() + " : " + me.getValue() );
        			}
     
     
                // Close buffered reader 
                in.close();
            } catch (Exception e) { 
                e.printStackTrace();
    	    System.out.println("Datei nicht gefunden!");
                System.exit(1);
            } 
    } 
    }

Similar Threads

  1. generate keys using j2ssh??????
    By rachana in forum Java Theory & Questions
    Replies: 2
    Last Post: February 3rd, 2011, 08:36 AM
  2. How do you generate javadoc?
    By javapenguin in forum Java IDEs
    Replies: 15
    Last Post: November 27th, 2010, 09:12 PM
  3. Generate prime numbers within fiboncacci series?
    By Manish87 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 7th, 2010, 12:24 PM
  4. How to generate an ActionEvent progrematically
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: May 22nd, 2010, 12:31 PM