0

I have this text file named params.txt which consist of:

3
WENSY WENDY
PING PING
CHLOE CHLOE

My question is, how can I read this inputs and compare the two strings in one line? Compare Wensy and Wendy and so on..

Code:

public class MainActivity extends Activity {

    InputStream is=null;
    DataInputStream dis=null;
    Resources myResources = null;
    TextView tv=null;
    String someText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button verify = (Button) findViewById (R.id.verify);
    myResources = this.getResources();
    tv = (TextView)findViewById(R.id.FileViewer);
    is = myResources.openRawResource(R.raw.params);
    dis = new DataInputStream(is);
    someText = null;

    verify.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try{
                someText=dis.readLine();
                }catch(IOException ioe){
                someText ="Couldn't read the file";
                }
                tv.setText(someText);           
        }
    });


    }


}

Can I save the input text on a String variable? Or there's another simple way to do this? thankyou :))

pingboo23
  • 137
  • 1
  • 3
  • 15

3 Answers3

2

Try , split()

String[] splittedValues = someText.split(" ");
if (splittedValues[0].equalsIgnoreCase(splittedValues[1]))
  {
      // do your process
  }
newuser
  • 8,338
  • 2
  • 25
  • 33
1

You can read the answers here to figure out how to read the file line by line.

Then to compare the two parts of each line you can seperate them by doing this:

String[] parts = line.split(" ");

Then you can compare each part using parts[0] and parts[1]. Example:

if(parts[0].equals(parts[1])) {
    doSomething();
}
Community
  • 1
  • 1
0

After your someText = dis.readLine();

String[] split = someText.split(" ");
if (split[0].equals(split[1]){
    Log.i("MainActivity", "strings are equal");
}else{
    Log.i("MainActivity", "strings are not equal");
}

Will compare to see if the strings are equal. otherwise, you could use String.compareTo(otherString); for comparisons.

panini
  • 2,026
  • 19
  • 21