Getting prob in following code.Can anybody suggest how to write if/else if I wan
ID: 3740920 • Letter: G
Question
Getting prob in following code.Can anybody suggest how to write if/else if I want to show hint as username/Paswword required if null and password or username did not match if entered wrong and go to next activity if its right.?? public class MainActivity extends AppCompatActivity { EditText userName, pwd; DatabaseHelper helper = new DatabaseHelper(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClickOk(View v) { if (v.getId() == R.id.LogIn) { final EditText User = (EditText) findViewById(R.id.userName); String t1 = User.getText().toString(); final EditText pawd = (EditText) findViewById(R.id.pwd); String t2 = pawd.getText().toString(); String password = helper.searchpass(t1); if (t2.equals(password)) { Intent lo = new Intent(MainActivity.this, Timer.class); startActivity(lo); if (t2!=password){ Toast.makeText(MainActivity.this, "Pass does'nt match", Toast.LENGTH_LONG).show(); }} //if(t1==null && t2==null) { else { User.setHint("Username Required"); pawd.setHint("Password Required"); }
Explanation / Answer
Hello, since you have provided very little details, I can provide limited help. But I assure you this solution will solve your problem. The main problems in your code are some misplaced curly braces and some wrong conditional checks. I assume that you put your code to call the onClickOk() inside xml document. And I suppose the DatabaseHelper class is working fine.
The code will fetch texts from username and password fields, checks if the password is correct, and if it is correct, it will move to the next activity, else it will first check if the fields are empty (not to be confused with null, both are different), if yes, display the hint messages in proper fields, else display the password wrong message via Toast.
Here is the corrected code. Output not included as I don’t have all the required inputs.
public class MainActivity extends AppCompatActivity {
EditText userName, pwd;
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClickOk(View v) {
if (v.getId() == R.id.LogIn) {
final EditText User = (EditText) findViewById(R.id.userName);
String t1 = User.getText().toString();
final EditText pawd = (EditText) findViewById(R.id.pwd);
String t2 = pawd.getText().toString();
String password = helper.searchpass(t1);
if (t2.equals(password)) {
/**
* Right password, moving to the next activity
*/
Intent lo = new Intent(MainActivity.this, Timer.class);
startActivity(lo);
} else {
/**
* Wrong or empty username/password
*/
if (!t2.equals("")) {
/**
* Password is not empty, but wrong
*/
Toast.makeText(MainActivity.this, "Pass does'nt match",
Toast.LENGTH_LONG).show();
}
if (t1.equals("")) {
/**
* user name is empty
*/
User.setHint("Username Required");
}
if (t2.equals("")) {
/**
* Password is empty
*/
pawd.setHint("Password Required");
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.