2 Replies - 398 Views - Last Post: 15 September 2015 - 08:18 AM Rate Topic: -----

#1 mrlee51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-September 15

Store in array as null but System.out.println gives the correct value?

Posted 14 September 2015 - 09:59 PM

Here is a snapshot of after I stored the keys and values in the map "result". I am trying to transverse the Collection to store in a String array and return it as the output.



                Map<String, Set<String>> result = new TreeMap<String, Set<String>>();


                //.... code here skipped because System.out.println(text) gives the expected answer, but output isn't storing what I want.

		String[] output = new String[numKeys]; 
		//numKeys is the number of keys in map

		Set<String> key = result.keySet();
		Collection<Set<String>> values = result.values();
		
		Iterator<Set<String>> it = values.iterator();

		for(String k : key)
		{
			String text = k + " ==> ";
			int index = 0;
			int count = 0;

			if(it.hasNext())
			{
				Set<String> element = it.next();
				
				for(String v : element)
				{
					count++;
					if(count < element.size())
						text += v + ", ";
					else
						text += v;	
				}
			}
			System.out.println(text); //this works
			
			output[index] = text; //this doesn't?
			index++;
		}
		
		return output;


System.out.println(text) gives me the correct result:
Annotation ==> BLAST, X
Genome ==> BLAST
Sensitivity ==> BLAST


But after calling the method from the main method and printing out "output" gives a wrong answer:
Sensitivity ==> BLAST
null
null



I'm not quite sure how else I could approach this, because should the output store exactly what text is printing out?

Is This A Good Question/Topic? 0
  • +

Replies To: Store in array as null but System.out.println gives the correct value?

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3562
  • View blog
  • Posts: 16,236
  • Joined: 20-September 08

Re: Store in array as null but System.out.println gives the correct value?

Posted 15 September 2015 - 12:51 AM

That's quite convoluted code. try this:

    public void printValues(Map<String, Set<String>> m) {
        for (Map.Entry<String, Set<String>> e : m.entrySet()) {
            System.out.printf("%s==>%s%n", e.getKey(),
                e.getValue().toString().replaceAll("^\\[|\\]$", ""));
        }
    }


This post has been edited by g00se: 15 September 2015 - 12:56 AM
Reason for edit:: Formatting bork

Was This Post Helpful? 1
  • +
  • -

#3 mrlee51   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-September 15

Re: Store in array as null but System.out.println gives the correct value?

Posted 15 September 2015 - 08:18 AM

View Postg00se, on 15 September 2015 - 12:51 AM, said:

That's quite convoluted code. try this:

    public void printValues(Map<String, Set<String>> m) {
        for (Map.Entry<String, Set<String>> e : m.entrySet()) {
            System.out.printf("%s==>%s%n", e.getKey(),
                e.getValue().toString().replaceAll("^\\[|\\]$", ""));
        }
    }



Thank you for your response!

I figured out why my code was wrong, and the error was that I let the index value to be zero every iteration of the loop, which is a trivial fix.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1