Get EditText value word by word

95 views Asked by At

Please can someone help me out with this code. I am a newbie programmer and i am trying to build a sample android project that extracts emails from texts in a text field. The .xml file is in place but the mainActivity is the problem.

Please do take a look at the code below and see the problem.

import android.app.*;
import android.os.*;
import android.view.*;
import android.content.*;
import android.widget.*;
import java.util.*;

public class MainActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }

    public void OnExtractButtonClick(View view)
    {
        EditText mainEditText1=(EditText)findViewById(R.id.mainEditText1);
        String txt = mainEditText1.getText().toString();     //Here, this code reads the
                                                                                               //whole input in the text 
                                                                                               //field (mainEditText1)...
     /* ..and that is my problem is at this point. How do i get to read the contents of 
       the text field(mainEditText1) word after word, (like .next() method does, as it 
       can only read input in the string till the next space and not all the input in
       the string.)
     */

       if  (txt.contains("@"))
       {
            Toast.makeText(MainActivity.this,"Let's see:  " +txt,
            Toast.LENGTH_LONG).show();
        }
    }
}

Thanks y'all

1

There are 1 answers

1
JDC On BEST ANSWER

Use String.split() to split your text into words:

String[] words = s.split("\\s+");
for(String word : words) {
    // Do what you want with your single word here
}

Note:
The expression \\s+ is a Regular Expression that will split your string by whitespace characters.