0

so what I am trying to accomplish is check a file to see if a word in the file matches the customers input.

I am attaching the code from when the button is clicked and what its supposed to do. Where its failing, or basically not doing it right, is where its supposed to check to see if it is in the list.

*CODE*
btn1.setOnClickListener(new OnClickListener() {  // listen for when i click
        public void onClick(View v) {
            btn1.setText("yeah");
            theirpass = edt1.getText().toString().toLowerCase(); // set theirpass = to text boxs input

              System.out.println("before i read file theirpass =s "+ theirpass);
              System.out.println("before i read file checking =s "+ checking);

              readFile(); // read the accouts dude


             tv1.setText("i pressed the button");//  test to make sure button pressing works
             System.out.println("after reading. theirpass =s " + theirpass);
             System.out.println("after i read file. checking =s "+ checking);

        }// end on click


    }); //end listener


}// end oncreate


public void readFile() {
BufferedReader reader = null;
try {
    reader = new    BufferedReader(
        new.  InputStreamReader(getAssets().open("account.txt")));

    // do reading, usually  loop until end of file reading  
    String mLine =  reader.readLine();// reading each line.

    while (mLine != null) {
        //process line

        mLine =   reader.readLine();

        if. (mLine.matches(theirpass)) { // when an. account is found stop reading
             System.out.println("cheking mLine"+ mLine);
            checking  = theirpass; //trying to set vale of. checking to theirpass
              Log.d("if statement in mline",checking);
             mLine=null;// trying to stop the loop

        }//end if
    }// end loop

} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
        try {
             reader.close();
        } catch  (IOException e) {
            //log  the exception
                    Log.d("Catch buffer","i failed the catch  in finally for reading lines");
        }
    }

}//end buffer reader

Toast.makeText(this,checking,Toast.LENGTH_LONG).show(); //test to get. value of cheking, keeps coming up blank-  no text
if (checking == theirpass) {
    btn1.setText("its  there");//test to see if the if statment  works

    System.out.println("i just checked if its there");//another test
} 


  }

 } 

to help with the code

To help with code

![helping with code again][2]

new Code

     if (mLine.equals(theirpass)) { // when an account is found stop reading
            System.out.println("cheking mLine"+ mLine);
            checking.equals(theirpass); //trying to set vale of checking to theirpass
            Log.d("if statement in mline",checking);
            mLine=null;// trying to stop the loop

        }//end if


        mLine = reader.readLine();
    }  //end buffer reader

value of theirpass

     //the value if theirpass is shown     below it's in the onClick method
    theirpass = edt1.getText().toString().toLowerCase(); // set theirpass = to text boxs input
baron dune
  • 387
  • 1
  • 5
  • 23

1 Answers1

0

Try printing the mLine value right after "mLine = reader.readLine();" to know what you read from the file.

But I guess your biggest problem is the misuse of the "matches" function. Matches uses reg ex (http://developer.android.com/reference/java/lang/String.html#matches(java.lang.String) ) . Instead you should use ".equals" or read this for a good use of string comparison.

You should also change your while loop the this:

 // do reading, usually  loop until end of file reading  
String mLine =  reader.readLine();// reading each line.

while (mLine != null) {
    //process line

    if. (mLine.matches(theirpass)) { // when an. account is found stop reading
         System.out.println("cheking mLine"+ mLine);
        checking  = theirpass; //trying to set vale of. checking to theirpass
          Log.d("if statement in mline",checking);
         mLine=null;// trying to stop the loop

    }//end if

    mLine =   reader.readLine();
}// end loop

Because you read a line, go into your while, read a new line and have now skipped the first line of the file.

Community
  • 1
  • 1
DeGoosseZ
  • 628
  • 5
  • 20