Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a String array that contains the "_" characheter in each element of it, I want to get rid of these characters.

I can do this task simply by using String [] split(String regex) method, but I don't want to it by this function because I will use this code in J2ME later.

I have write a code to do this task but the output are strange characters [Ljava.lang.String;@19821f [Ljava.lang.String;@addbf1 !!!

public class StringFragementation {

static public   String [] mymethod(String [] mystring)
{

   String ss [] =new String[mystring.length];

   for(int j=0;j<mystring.length;j++)

   {
   ss[j] =  mystring[j].replace('_',',');

   }

   return ss ;

   }
 public static void main(String [] args)

  {

   String []  s = {"Netherlands_Iceland_Norway_Denmark","Usa_Brazil_Argentina"};

   for(int i=0;i<s.length;i++)
   {

 System.out.println("" + mymethod(s) );

   }

       }

       }  
share|improve this question

3 Answers

up vote 2 down vote accepted

I have modified your code. It will give you desired output. use following code

public static void main(String[] args) {
    String[] s = {"Netherlands_Iceland_Norway_Denmark", "Usa_Brazil_Argentina"};
    String[] finalString = mymethod(s);        
    for (int i = 0; i < s.length; i++) {
        System.out.println("" + finalString[i]);
    }
}

static public String[] mymethod(String[] mystring) {
    String ss[] = new String[mystring.length];
    for (int j = 0; j < mystring.length; j++) {
        ss[j] = mystring[j].replace('_', ',');
    }
    return ss;
}
share|improve this answer

In Java, each object has toString() method, the default is displaying the class name representation, then adding @ and then the hashcode.

ss is an array of Strings. You should use Arrays#toString(), which is implemented this way:

3860     public static String toString(int[] a) { {
3861        if (a == null)
3862            return "null";
3863        int iMax = a.length - 1;
3864        if (iMax == -1)
3865            return "[]";
3866
3867        StringBuilder b = new StringBuilder();
3868        b.append('[');
3869        for (int i = 0; ; i++) {
3870            b.append(a[i]);
3871            if (i == iMax)
3872                return b.append(']').toString();
3873            b.append(", ");
3874        }
3875    }

Or, you can do:

for(String str : mymethod(s)) {
    System.out.println(str);
}
share|improve this answer
 
I cann't use Arrays object in j2me version any alternative solution plz? –  Elhadi Mamoun Nov 10 at 10:08
 
@ElhadiMamoun See "Or, you can do:". –  Maroun Maroun Nov 10 at 10:09
 
look util.Arrays not found in J2me edition –  Elhadi Mamoun Nov 10 at 10:12
 
You don't have to... See my second suggestion....... –  Maroun Maroun Nov 10 at 10:13

What you see there is the result of the toString() method when invoked on arrays. It's almost meaningless. What is printed is the type of the array followed by its hashCode.

Use java.util.Arrays.toString() to transform an array into a meaningful String representation.

share|improve this answer
 
I cann't use Arrays object in j2me version any alternative solution plz? –  Elhadi Mamoun Nov 10 at 10:08
 
Use a for loop. –  JB Nizet Nov 10 at 10:08
 
util.Arrays package not found in J2me edition –  Elhadi Mamoun Nov 10 at 10:12
1  
Yes. So use a for loop. for (String s : array) {System.out.println(s);} –  JB Nizet Nov 10 at 10:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.