can I ask how I can instantly equal my TextField named txtUserName into 'Aime' and also my txtPassword equal to 'Joy' so that it will show the message "User Name and Password Match!" ? Anyone? Please help me :(
public void actionPerformed(ActionEvent e){
if (e.getSource()== btnClear){
txtUserName.setText("");
}
if(e.getSource() == btnLogin){
if (txtUserName.setText="Aime" && txtPassword.setText="JOy")){
JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}
else {
JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}
These are the Small modifications needed.
if (txtUserName.getText().equals("Aime") && txtPassword.getText().equals("Joy")){
JOptionPane.showMessageDialog(null, "User Name and Password Match!");
}else{
JOptionPane.showMessageDialog(null, "User Name and Password Invalid!");
}
For your reference : What is the difference between == vs equals() in Java?
you have two problems here:
txtUserName.setText="Aime" && txtPassword.setText="JOy"
am guessing you are trying to check the user input so you need to use the gettext method...
on the other hand you can not compare strings using ==, but furthermore you mistyped the == and are instead of comparing assigning, assignment that is invalid since you can not set the text of that view in that way...
try instead
txtUserName.getText().toString().equals("Aime") && ...
Related
I'm unsure of best practices with JOptionPanes, though this could equally just be a logic problem. I want an input box that asks for a name, checks it is a letter only string, and user can cancel.
I understand that cancelling a JOptionPane results in returning a null, which I've implemented at the start. The issue is that if a user enters incorrectly in the first pane, they cannot cancel from the second.
EDIT: worth pointing out that if user cancels I don't want to do anything with the name. This is the issue that forces me into the loop. Logic should be to check user input; if ==2 do nothing. If something else, validate is a word and use it. Loop around if not valid. The problem is the user can cancel later and the action of using the name is actioned anyway since it's in the second loop, with the value of 2.
I currently have:
JOptionPane optionPane1 = new JOptionPane(text, OK_OPTION, CANCEL_OPTION);
optionPane1.setWantsInput(true);
JDialog d1 = optionPane1.createDialog(null);
d1.setVisible(true);
name = optionPane1.getInputValue().toString();
if(name == null){
gamePaused = true;
}
else{
while(!name.matches("^[a-zA-Z]+$") || name.length() == 0){
JOptionPane optionPane2 = new JOptionPane("Please enter a word.\nTry again.", OK_OPTION, CANCEL_OPTION);
optionPane2.setWantsInput(true);
JDialog d2 = optionPane2.createDialog(null);
d2.setVisible(true);
name = optionPane2.getInputValue().toString();
}
///use name
}
Is there a better way of doing this, so that I can allow a user to cancel and escape the loop?
You can replace all your code with this one
do {
name = JOptionPane.showInputDialog(null, "Enter name here : ", "title", JOptionPane.OK_CANCEL_OPTION);
} while(name != null && !name.matches("^[a-zA-Z]+$"));
if (name == null) {
gamePaused = true;
} else {
//Do whatever want with the name
}
The problem is that inside your while loop, there is no further check whether the user pressed cancel (which means name == null). Once the user enters something invalid in his first attempt, he stays inside of the while loop and is asked for his name over and over again until there is a valid input, and the program does not care anymore about whether he clicked cancel. Therefore, you need to add before name = optionPane2.getInputValue().toString();:
if (optionPane2.getInputValue() == null) {
gamePaused = true;
break;
}
Inside your while loop, check if cancel is selected by user:
Add this inside the while loop:
if(optionPane2.getValue().toString().equals ("2")){ // 2 for close option in your case
break;
}
This is a method within my program and I cannot use a bunch of if-else statements for my code. I need alternatives.
public String bedRoom1(){
int newChoice10 = JOptionPane.showConfirmDialog(null,"Would "
+ "you like to explore this room?",
"question",JOptionPane.YES_NO_OPTION);
if (newChoice10 == JOptionPane.YES_OPTION){ //if the user would lieke to
// explore then they are given the option for which objects they woul like to explore
int newChoice4 = JOptionPane.showConfirmDialog(null,"Explore"
+ " the Rocking chair(type YES) or Window(type NO)?"
, "question",JOptionPane.YES_NO_OPTION);
//The following if else statments are still within
//the first statement of the first if statement
if (newChoice4 == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null,"Chair starts "
+ "rocking with no one in it");
}else{
JOptionPane.showMessageDialog(null, "You see a child outs"
+ "ide on a swing and he suddenly vanishes.");
}
}else if (newChoice10 == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null,"You have chosen not "
+ "to explore Bedroom 1 and therefore continue "
+ "into the following room");
}
return null;
}
Can you please provide examples. Thank you in advance.
you can use showOptionDialog with all your choices instead of showConfirmDialog
and use switch - case instead of if then else
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I'm working on a project with a registration form. The form asks the user to
enter a password of their choice in a JPassword Field, and to enter it again in another JPassword Field.
I'm using a JOptionPane to prompt the user if the passwords do not match. but when i use passwordField.getPassword().toString() on the two, they don't match.
I have tried entering a basic "12345" for example on both but i still have no luck.
i know you should use .equals(), but what is the equivalent to this for "not equals" without using the "!=" operator.
here is the following code.
if(e.getSource() == submit)
{
String name = nameTextField.getText();
String address = addressTextField.getText();
String phoneNumber = numberTextField.getText();
String dob = datePicker.getDateFormatString();
String password = passwordField.getPassword().toString();
String password2 = passwordFieldTwo.getPassword().toString();
try
{
//If the user leaves the field empty
if(name == null || address == null || phoneNumber == null || dob == null
|| password == null || password2 == null)
{
//Error message appears to prompt the user to
//complete the form
JOptionPane.showMessageDialog(null, "All fields must be complete to submit.", "Woops", JOptionPane.ERROR_MESSAGE);
}
if(password != password2)
{
JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
passwordField.setText(null);
passwordFieldTwo.setText(null);
}
Any help with this would be much appreciated.
The != related to equals() for String comparison is expressed by !x.equals(y)
As an example lets take your code, to see if the two passwords not match do the following:
if (!Arrays.equals(passwordField.getPassword(), passwordFieldTwo.getPassword())) {
JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
}
I am trying to cover possible options when using the JOptionPane.showInputDialog box.The user must enter a "Y" to continue running the code, "N" will cancel the procedure and clicking the cancel button should do the same as typing "N". But, when the user clicks cancel, I want to show the a message like "You have chosen to cancel the order" before the System.exit(0) runs. I have not been able to get that message to display. Below is the code I have so far:
inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)");
if(inputStr.equalsIgnoreCase("N")){
JOptionPane.showMessageDialog(null, "Since you are not entering an order....\n" +
"The program will close.");
System.exit(0);
}
else if(inputStr.equals(null)){
JOptionPane.showMessageDialog(null, "You have chosen to cancel this order");
System.exit(0);
}
else if(!inputStr.equalsIgnoreCase("Y")){
JOptionPane.showMessageDialog(null, "You have entered an invalid character.\n" +
"Enter a 'Y' or 'N' only.");
continue;
}
I would use the YES_NO_CANCEL_OPTION:
Object[] options = {"Yes","No","Cancel"};
int n = JOptionPane.showOptionDialog(frame,
"Continue?",
"Would you like to continue?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[2]);
if (n == JOptionPane.YES_OPTION) {
System.out.println("Clicked Yes");
} else if (n == JOptionPane.NO_OPTION) {
System.out.println("Clicked No");
} else if (n == JOptionPane.CANCEL_OPTION) {
System.out.println("Clicked Cancel");
} else {
System.out.println("something else (like clicked the 'x' button)");
}
Try changing inputStr.equals(null) to inputStr == null
Works fine as long as you change the if statments around to what they are below.
String inputStr;
inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)");
if(inputStr == null){
JOptionPane.showMessageDialog(null, "You have chosen to cancel this order");
System.out.println("hello");
System.exit(0);
}
else if(inputStr.equalsIgnoreCase("N")){
JOptionPane.showMessageDialog(null, "Since you are not entering an order....\n" +
"The program will close.");
System.exit(0);
}
else if(!inputStr.equalsIgnoreCase("Y")){
JOptionPane.showMessageDialog(null, "You have entered an invalid character.\n" +
"Enter a 'Y' or 'N' only.");
}
Think about
if(inputStr.equals(null)){
Does it make sense to call the 'equals()' method on a 'null' object? Because if inputStr is null, then you would not be able to call a method on it.
The correct syntax would be:
if(inputStr == null){
and do this as the first 'if', to protect you from a NPE.
This what i will do
String inputStr = JOptionPane.showInputDialog("Enter an order (Y/N)");
if (inputStr == null || inputStr.isEmpty()) {
JOptionPane.showMessageDialog(null, "You Cancelled");
} else {
if (inputStr.equalsIgnoreCase("N")) {
JOptionPane.showMessageDialog(null,
"Since you are not entering an order....\n"
+ "The program will close.");
System.exit(0);
} else if (!inputStr.equalsIgnoreCase("Y")) {
JOptionPane.showMessageDialog(null,
"You have entered an invalid character.\n"
+ "Enter a 'Y' or 'N' only.");
}
}
Goodluck
Make a delay in between exit(0) and message with javax.swing.Timer
And Change
if(inputStr.equals(null)){
with
if(inputStr == null){
== will always compare for identity - i.e. whether the two values are references to the same object. This is also called reference equality. Java doesn't have any user-defined operator overloading.
.equals() will call the virtual equals method declared by Object, unless a more specific overload has been introduced by the compile-time type of inputStr.
Of course, if inputStr is null then you'll get a NullPointerException when you try to call inputStr.equals(null).
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i have a username and a password string. How can I compare them in Android? This is somewhat what I'm looking for:
if( user== username && pass == password){ then do this }
else if(user[does not equal to]username && pass==password{ toast (invaild username.)}
else if(user==username && pass[does not equal] password{toast(invaild password)}
else{ toast [invaild login]}
I know that's not correct coding but the two "else if" statements is what I'm looking for. The rest is just to give a better understanding of what I'm trying to do.
The username is what's entered into a Edittext same with the Password so they are both String when i .getText.toString right?
To compare the text in strings use equals() e.g. pass.equals(password) rather than == which checks if two strings are the same object.
For "does not equal" you can negate the statement e.g. !pass.equals(password).
Use the equals method for comparing strings.
if( user.equals(username) && pass.equals(password))
{
then do this
}
else if(!user.equals(username) && pass.equals(password)
{
Toast.makeText(this,"invalid username",Toast.LENGTH_SHORT).show();
}
else if(user.equals(username) && !pass.equals(password))
{
Toast.makeText(this,"invalid password",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this,"Invalid login",Toast.LENGTH_SHORT).show();
}
You want to use .equals(..) to compare Strings. Also, to do not logically, you can use !.
//define user, username
//define pass, password
if(user.equals(username) && pass.equals(password)){
//then do this
}
else if(!user.equals(username) && pass.equals(password)){
//toast (invaild username.)
Toast.makeText(getApplicationContext(), "invalid username", Toast.LENGTH_SHORT).show();
}
else if(user.equals(username) && !pass.equals(password)){
//toast(invaild password)
Toast.makeText(getApplicationContext(), "invalid password", Toast.LENGTH_SHORT).show();
}
else{
//toast [invaild login]
Toast.makeText(getApplicationContext(), "invalid login", Toast.LENGTH_SHORT).show();
}
Here are some sources on Toast:
http://www.mkyong.com/android/android-toast-example/
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
You could use pass.equal(password).