I am not able to toast messages in Android Studio - java

I have created an add button. No data is entered in the EditText field and when the user press the button I am not able to Toast the message.
If(text1.getText( ).toString( ).matches(" ") || text2.getText( ).toString( ).matches(" "))
{
Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT.show( );
}

you can try this
EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
Toast.makeText(MainActivity.this, "You did not enter a username", Toast.LENGTH_SHORT).show();
}

You are matching wrong string. Instead of .match(" ") use .match("") or it is better to use text1.getText().toString().isEmpty(). In fact your if block never reached.

Try changing your code and IF condition like this:
If (String.valueOf(text1.getText()).equals("") || String.valueOf(text2.getText()).equals(""))
{
Toast.makeText(MainActivity.this,"input values",Toast.LENGTH_SHORT).show();
}
That's all,
Hope it helps :)

Use trim() this function will remove spaces.
If(text1.getText( ).toString( ).trim().equals("") || text2.getText( ).toString.trim().equals(""))
{
Toast.makeText(MainActivity.this,"You did not enter a username and password",Toast.LENGTH_SHORT.show( );
}
I hope this will helpful.

Related

Can you make a JOptionPane.showMessageDialog appear after a duration of time?

I have a question regarding how someone was to make a JOptionPane.showMesageDialog(); appear within a duration of time. For example, my program is supposed to ask about the user's favorite movie, Subsequently at least 15 seconds after the user answers, my program is supposed to ask "are you there?", giving them the option of replying with Yes, no, or maybe.
How exactly can I do that? (if that's possible)
here's my code
```
if (null==moviename|| moviename.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "You did not enter anything");
} else { JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");
}
String ques=JOptionPane.showInputDialog("Are you there? Yes? No? Maybe?");
if (ques=="yes") {
JOptionPane.showMessageDialog(null,"Great To hear!");
} else if (ques=="no") {
JOptionPane.showMessageDialog(null,"Thats odd..");
} else if (ques=="maybe" || ques=="Maybe" ) {
JOptionPane.showMessageDialog(null, "That was rhetorical...");
}
}
```
I belive you just need to add Thread.sleep(n); before the JOptionPane
remember to import: java.lang.Thread;
time is in ms so 15seconds is gonna be 15000.
if (null==moviename|| moviename.trim().isEmpty()) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
System.out.print("Sleep problem of type: "+e);
}
JOptionPane.showMessageDialog(null, "You did not enter anything");
} else {
JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");
}

Two if statement is executed in java code

Im currently trying to do a validation for staff login form. but i realized in my validation output that whenever i enter a value to the username text field, it still pops up the message "please enter your username" then the "invalid credentials"message box. Here is my code down below :
String username = usernameTxt.getText();
String password = passwordTxt.getText();
if (username.contains(""))
{
JOptionPane.showMessageDialog(null,"Please Enter Your Username Credentials.");
}
else if (password.contains (""))
{
JOptionPane.showMessageDialog(null,"Please Enter Your Password Credentials.");
}
else if (password.contains ("") && (username.contains("")))
{
JOptionPane.showMessageDialog(null,"Please Enter Your Login Credentials.");
}
if ((username.contains("staff") && password.contains ("pass")))
{
JOptionPane.showMessageDialog(null,"Login Successfull","Success",JOptionPane.INFORMATION_MESSAGE);
passwordTxt.setText(null);
usernameTxt.setText(null);
staffdashboard sd = new staffdashboard();
sd.setVisible(true);
this.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Login Details","Login Error",JOptionPane.ERROR_MESSAGE);
passwordTxt.setText(null);
usernameTxt.setText(null);
}
enter user message box
invalid login details message box
What am i missing out and how do stop the form to output 2 message box at once?
The problem is in the first if statement: username.contains("")
Each String contains empty String.
You should replace it with if("".equals(username))
Or use StringUtils.isBlank(username);
And the same for all the contains("")
#Naya's answer is correct, however you may want to remove any empty spaces assuming they are not allowed as valid input, so :
if (username.trim().equals("")) ...
else if (password.trim.equals("")) ...
...

Java Swing Dialog issue

When pressed the "Inregistrare" button a dialog pops, requesting the user to enter a password (set to "qwerty"). I want it keep displaying dialogs until the password is correct. The method is the following:
private void ItemInregistrareActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane dialog = new JOptionPane();
dialog.setWantsInput(true);
dialog.showInputDialog("Password please:");
while(dialog.getInputValue()!="qwerty")
dialog.showInputDialog("Mai baga o fisa.");
ItemInregistrare.setEnabled(false);
ItemOpen.setEnabled(true);
ItemSave.setEnabled(true);
}
The problem is it never gets out of the while, even if the password is correct. Any tips?
JOptionPane.showInputDialog is a static method and does not need any instance of JOptionPane. Moreover, it already returns the entered value or null if user pressed Cancel. So you don't need to call dialog.getInputValue().
You could try something like this:
String pwd;
do {
pwd = JOptionPane.showInputDialog("Password please:");
} while (pwd != null && !pwd.equals("qwerty"));
if (pwd == null) {
JOptionPane.showMessageDialog(null, "You pressed cancel");
} else {
JOptionPane.showMessageDialog(null, "Password is correct");
}
Try using
!dialog.getInputValue().equals("qwerty")
to compare strings

Why does it go to exception when parse to integer?

I canĀ“t figure out why this Integer.parseInt makes an exception, its a NullpointerException..
try
{
int numberOfPictures = Integer.parseInt(editTextNumberOfGames
.getText().toString());
Toast toast =Toast.makeText(getBaseContext(), "ratt", Toast.LENGTH_SHORT);
toast.show();
} catch (Exception e)
{
Toast toast =Toast.makeText(getBaseContext(), "fel", Toast.LENGTH_SHORT);
toast.show();
}
Have you used a debugger to verify that editTextNumberOfGames is not null when you get to this code?
You can temporary break up the Integer.parseInt(editTextNumberOfGames.getText().toString()) statement to see exactly which part of the compound statement is causing the exception.
See http://developer.android.com/reference/java/lang/Integer.html
As ratchet freak pointed out in comments, i had not done this:
editTextNumberOfGames = (EditText)findViewById(R.id.editTextNrOfPictures);
Thanks for all help though.
It throws an expecption because editTextNumberOfGames is null
initialize editTextNumberOfGames correctly, or if for whatever reason you cannot garuantee, that it is initialized then
int numberOfPictures;
if (editTextNumberOfGames != null) {
numberOfPictures = Integer.parseInt(editTextNumberOfGames.getText());
}

String and EditText function issues in Android

i'm trying to trigger a conditional by checking the user input in an EditText field. when i print the String from the EditText to logcat, i can see the data change, but the String functions that check against the values always return false.
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
this code always displays "Missing Driver ID!". i have tried these other conditionals, with no success:
(((EditText)findViewById(R.id.drv_in)).getText().toString().isEmpty()) //does not compile, says cannot find symbol, but the function is in the Android documentation
(((EditText)findViewById(R.id.drv_in)).getText().toString().length() < 1) //returns false, even for strings of length > 1
i can confirm that the data is, indeed, no null by looking at logcat and seeing my data show up in the logs. what's wrong with the conditional?
it doesn't fail if you insert no data in the first transmit. if the first transmit fails, all subsequent transmissions fail, regardless of whether you change the data or not. furthermore, if it passes the first transmission, it will pass all subsequent transmissions.
additionally, there are other conditionals, posted in the full code below, which also evaluate only on the first click of the button.
transmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//data validation
///////////////////////
boolean valid = true;
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
if(custSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Customer Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(prdSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Product Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("CAR") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("AUTO") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("TRUCK")
) {
//must have make, license# and 1vin
if(((EditText)findViewById(R.id.make_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Make/Model!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.tag_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Tag Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.vin1_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin2_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin3_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin4_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin5_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin6_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin7_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin8_in)).getText().toString().equals("")
) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing VIN Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
}
//Log.e("smsDRVERR",((EditText)smsActivity.this.findViewById(R.id.drv_in)).getText().toString());
//begin transmission
///////////////////////
if(valid) {
showDialog(DIALOG_TX_PROGRESS);
Thread t = new Thread(txRunnable);
t.start();
} else {
//do things if needed
}
}
I'd post this as a comment, but it'd be too long...
I don't think the problem is what you think it is. However, I can't say what the problem is, because you haven't been clear about how you're detecting success and/or failure.
Let's start by clarifying the diagnostic code, to remove any possible ambiguities. I'd suggest you change this:
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
to:
final String drv = (EditText)findViewById(R.id.drv_in)).getText().toString();
if(drv.equals("") {
TX_FAIL_TEXT = "Missing Driver ID!";
Log.e("SMSDRVERR", "Missing ID " + drv);
}
else {
Log.e("SMSDRVERR", "Found ID" + drv);
}
This will eliminate any possible ambiguity in the log about whether the text really was missing. (It also makes for more readable code.)
the problem was actually with the Dialog objects. the conditional is fine. at the beginning of the onClick method, i added a call to:
removeDialog(DIALOG_FAIL);
this forces Android to rebuild the Dialog the next time it is called.
EDIT: for future reference, there is a more elegant way to do this using onPrepareDialog(), but this solution was easier for me.

Categories