0

Hope you are well. Could you help me with the below code.

I would like to make sure that a user Inputs a value into the edtScore and it is not left blank before they hit the Save Button. Currently the user can hit the save button even if the edtScore is left blank.

Thanks for the Help.

public void saveLogOnClick (View view){
    String FILENAME = "results.csv"; //Stores onto Mobile Phone Storage Space
    String entry = edtDate.getText().toString() + "," +
                   edtTime.getText().toString() + "," +
                   category.getSelectedItem().toString() + "," + 
                   edtScore.getText().toString() +"\n"; 
    try{
        FileOutputStream out = openFileOutput(FILENAME, Context.MODE_APPEND); 
        out.write(entry.getBytes());
        out.close();
        toastIt("Entry Saved");

    } catch (Exception e){
        e.printStackTrace();
    }
}
M.S.R.92
  • 5
  • 4

1 Answers1

0

I'm a beginner, but I think this is easier than it appears. But for the sake of simplicity, this is what I can say at my level.

The easiest way to check if edtScore was empty would be to check if the string is blank, like so:

if( edtScore.getText().toString() == "" ){
    return; // exit failure
}

And then, with further research I could come up with:

String str = edtScore.getText().toString();
if(str != null && !str.isEmpty()){
  return;// exit failure.
}
Community
  • 1
  • 1
Andrew
  • 3,393
  • 4
  • 25
  • 43
  • Hi thanks @Lemony-Andrew, I tried your code but am not getting any luck. I am still able to save the records with blank editText. I have also inputted in a System.out.println("Please Input your Reading"); but this does not seem to appear whereas, the toastIt does. Any suggestion to what I may be doing wrong? – M.S.R.92 Mar 15 '15 at 21:08
  • @M.S.R.92 Did you try putting it at the front of your function? And if it's not empty you could try outputting the exact content of the edtScore string and see what it supposedly contains. – Andrew Mar 15 '15 at 21:14