user3163366
user3163366

Reputation: 1

Problems with EditText(... string_split ...)

My LogCat Output

01-05 21:04:22.299: D/info(2699): Lenght b = 0
01-05 21:04:22.299: D/info(2699): Lenght Split = 0
01-05 21:04:22.299: D/info(2699): Item = 25.8
01-05 21:04:22.299: D/info(2699): Länge = 4

My Question:

From my understanding Lenght b and Split should be 2 and b[0]=25 + b[1]=8...

b[0] + b[1] are empty...

What is the problem with my code?

java:

    public class ButtonCreateLocationOnClickListener implements OnClickListener {
        public void onClick(View v) {
            final EditText editTextLocationName = (EditText) formElementsView.findViewById(R.id.editTextLocationName);
            if ((editTextLocationName.getText().toString().length() > 0) && (StringDescription.length() > 0))){
                String Test = editTextLocationName.getText().toString();
                String[] b = Test.split(".");
                Log.d("info", "Lenght b = " + b.length);
                for (String string : b) {
                    Log.d("info", string);
                }
                String[] itemsName = editTextLocationName.getText().toString().split(".");
                Log.d("info", "Lenght Split = " + itemsName.length);
                Log.d("info", "Item = " + editTextLocationName.getText().toString());
                Log.d("info", "Länge = " + editTextLocationName.getText().toString().length());
            }
        }//onClick
    }//OnClickListener

Upvotes: 0

Views: 85

Answers (2)

Toon Borgers
Toon Borgers

Reputation: 3658

The String.split() accepts a regex as a parameter. If you look here you'll see that a dot is a special character in regex's.

Try escaping it like so: "45.2".split("\\.").

Upvotes: 0

Daniel
Daniel

Reputation: 869

You need to escape the dot, otherwise it will interpret it as if it was a RegularExpression.

Change the split line to String[] b = Test.split("\\.");

Upvotes: 1

Related Questions