0

My piece of code..

package com.xchanging.selenium.utility;

import java.io.IOException;
import java.util.LinkedHashMap;

import org.apache.commons.lang3.ArrayUtils;

public class DataProviderConvertor extends ReadExcel {

    public static Object[][] convertData(String sheetName, String testCaseName)
            throws IOException {

        LinkedHashMap<String, String> table = ReadExcel.getData(sheetName,
                testCaseName);
        String[] myStringArray = new String[table.size()];
        for (String key : table.values()) {
            System.out.println("Keyvalues " + key.toString());
            String value = key.toString();
            ArrayUtils.add(myStringArray, value);
        }
        System.out.println("1st Index: " myStringArray[0]);
    }
  }

It is returning

Keyvalues Y
Keyvalues ie
Keyvalues QC 
Keyvalues Yes
Keyvalues Rework Checklist Comments
Keyvalues Yes
Keyvalues MRI Updated Comments


1st Index: null

I am expecting 6 elements in this array but all are returning NULL.. Why it is not returning the expected values..??

2
  • where are you returning btw Commented Jan 12, 2015 at 6:47
  • @ChanGan just two changes required, code is fine Commented Jan 12, 2015 at 7:06

6 Answers 6

2

How about much simpler way.

public static Object[][] convertData(String sheetName, String testCaseName)
        throws IOException {

    LinkedHashMap<String, String> table = ReadExcel.getData(sheetName,
            testCaseName);

    String[] myStringArray = table.values().toArray( new String[ 0 ] );

    System.out.println("1st Index: " + myStringArray[0]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try

for (String key : table.values()) {
     System.out.println("Keyvalues " + key.toString());
     String value = key.toString();
     myStringArray =ArrayUtils.addAll(myStringArray, value);
}

Or

int cnt=0;

for (String key : table.values()) {
         System.out.println("Keyvalues " + key.toString());
         String value = key.toString();
         myStringArray[cnt++] =value;
    }

Comments

1

ArrayUtil.add method copies the array and then adds the new element at the end of the new copied array.

So, i think that's where the problem lies in your code.

That is why myStringArray is showing the size as 0.As the myStringArray is copied and a new string array is formed and then the element is added to it.

Comments

0

You can try this -:

int index =0;

for (String key : table.values()) {
            System.out.println("Keyvalues " + key.toString());
            String value = key.toString();
            myStringArray[index++] = value;            
        }

Comments

0
ArrayUtils.add(myStringArray, value);

This method creates and returns a new array with the value added to it.

You need to assign the result:

myStringArray = ArrayUtils.add(myStringArray, value);

It has to be done that way because arrays cannot be resized.

1 Comment

The method add(String[], String) is undefined for the type DataProviderConvertor
0

Just two changes required, code is perfectly fine otherwise.

1.) String[] myStringArray = new String[table.size()];, change to

String[] myStringArray = null;

2.) ArrayUtils.add(myStringArray, value); change to

myStringArray = ArrayUtils.add(myStringArray, value);

Rest you can read and debug about this method in API.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.