0

This my java code, here i have to add a string "RFID" in a String Array itemtypes and i have to stored it in an another string array item.But im getting an error.

  String[] itemtype;
  String[] item;
     \...........
      .........../
   try {
                response = (SoapPrimitive) envelope.getResponse();
                Koradcnos = response.toString();
            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            itemtypes = Koradcnos.split(";");
            item=itemtypes+"RFID";//here im getting error
        } catch (Exception er) {
            //Helper.warning("Error", er.toString(),this);
        }
         imageId = new int[itemtypes.length];
        for (int i = 0; i < itemtypes.length; i++)
            if (itemtypes[i].equals("Yarn")) {
                imageId[i] = R.drawable.yarnitem;
3
  • item is of the type String array, you cannot concatenate a String to it. You might want to use an ArrayList. Commented Jan 26, 2016 at 13:11
  • item[index] = itemtypes+"RFID"; should do the trick ;) Commented Jan 26, 2016 at 13:12
  • Still Im getting an error at index! What should i do now? How to rectify it ??Please give me a solution for this@shark Commented Jan 26, 2016 at 13:19

3 Answers 3

1

You need to work around for this :

Using with Arrays.copyOf

item =  Arrays.copyOf(itemtypes , itemtypes.length + 1);
item[item.length - 1] = "RFID";

Or direct split from Koradcnos

item = (Koradcnos + ";RFID").split(";");
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot concatenate string with a list String.split() method returns a String array.

Instead of item=itemtypes+"RFID" iterate through the array as:

for (int i=0; i < itemtypes.length();  i++) {
    item[i] = itemtypes[i] + "RFID";
}

Also I think the variable name will be itemtypes instead of itemtype

Comments

0

itemtypes is String array,it can't be modified,if you want to get a new array adding another element,you can use System.arrayCopy

System.arraycopy(Object src,  
             int  srcPos,
             Object dest, 
             int destPos,
             int length)

Like this:

item = new String[itemtypes.length+1];
System.arraycopy(itemtypes,0,item,0,itemtypes.length);
item[itemtypes.length] = "RFID";

2 Comments

It's showing an error(method call expected) at itemtypes.length()+1 in the above code.
@prabhu,length is property,not method.

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.