2

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) );

   }

       }

       }  

4 Answers 4

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

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);
}

2 Comments

I cann't use Arrays object in j2me version any alternative solution plz?
@ElhadiMamoun See "Or, you can do:".
0

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.

2 Comments

I cann't use Arrays object in j2me version any alternative solution plz?
Yes. So use a for loop. for (String s : array) {System.out.println(s);}
0
public class MyDisticntElements {

    public static void main(String[] args)
    {
        int [] nums = {2,3,4,5,8,7,7};
        MyDisticntElements.printDuplicate(nums);
    }
    public static void printDuplicate(int[] arr)
    {
        for(int i =0; i<arr.length; i++)
        {
            boolean duplicate = false;
            for(int j = 0; j<i; j++)
            {
                if(arr[i] == arr[j])
                {
                    duplicate = true;
                    break;
                }
            }
            if(duplicate)
            {
                System.out.println(arr[i]+"");
            }
        }
    }
}

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.