Concatenate String and variable in JOptionPane window [duplicate] - java

This question already has answers here:
How do I concatenate two strings in Java?
(23 answers)
Closed 6 years ago.
I have the following method in a test environment, I'm trying to attach the variable count "loginAttempts"
to the JOptionPane window so the user would know how many failed login attempts they've had so far.
Essentially, I want to concatenate another string and a variable to this line if possible .
JOptionPane.showMessageDialog(null, "Enter a valid username and a password. You've had ||loginAttempts|| "failed login attempts", "Error", JOptionPane.ERROR_MESSAGE);
Here's the method out of the application"
private void loginButton(java.awt.event.ActionEvent evt) {
username = userName.getText();
password = userPassword.getText();
if (username.equals(finalusername) && password.equals(finalpassword) && loginAttempts <= 3) {
loginSuccess = true;
} else {
loginSuccess = false;
loginAttempts = loginAttempts + 1;
}
if (loginSuccess) {
JOptionPane.showMessageDialog(null, "You have successfully logged in", "Success", JOptionPane.DEFAULT_OPTION);
} else if (loginAttempts > 3) {
JOptionPane.showMessageDialog(null, "You have had 3 failed login attempt, your account has been locked","Locked", JOptionPane.ERROR_MESSAGE);
} else if (!loginSuccess) {
JOptionPane.showMessageDialog(null, "Enter a valid username and a password", "Error", JOptionPane.ERROR_MESSAGE);
}
}

You can concatenate this way (replace the last else with that ) :
} else if (!loginSuccess) {
JOptionPane.showMessageDialog(null, "Enter a valid username and a password, Number of attempts : "+loginAttempts , "Error", JOptionPane.ERROR_MESSAGE);
}

Related

equals method doesn't work

I'm practicing a simple Java GUI by making a login system without a database, but my if statement using .equals doesn't work because it always says incorrect password.
contains() work but I think it's not that secure
String user = USERNAME.getText();
String pass = PASSWORD.getText();
if(user.equals("username") && pass.equals("password") ){
JOptionPane.showMessageDialog(null, "Welcome to GUI");
} else {
JOptionPane.showMessageDialog(null, "Wrong Username and password");
}
As #MadProgrammer pointed out that equals() is a case sensitive method.
"password1" is NOT EQUAL to "PASSWORD1" //The strings should match with case.
Also to avoid NullPointerException a better way is
String user = USERNAME.getText();
String pass = PASSWORD.getText();
if ("username".equals(user) && "password".equals(pass)) {
JOptionPane.showMessageDialog(null, "Welcome to GUI");
} else {
JOptionPane.showMessageDialog(null, "Wrong Username and password");
}

Validation when Creating an Account JAVA

So there is this certain part of my program where I can create an account and the created account will be inserted into my database. And I'm trying to code something where *refer to the code:
public void actionPerformed(ActionEvent e) {
String user = userField.getText().trim();
String pass = passField.getText().trim();
String conPass = confirmPass.getText().trim();
try{
// TODO Auto-generated method stub
if(e.getSource()==submit){
if (user.equals(user)&&pass.length()==0){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if the pass field is blank
else if(user.length()<5){
JOptionPane.showMessageDialog(null,"Username must be at least 5 characters!");
}
else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
getQuery(sqlLogin);
JOptionPane.showMessageDialog(null, "Account Successfully Created!");
create.dispose();
GUI gui = new GUI();
}//if(pass.equals(conPass))
else if(user.length()==0&&pass.length()==0){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if both fields are blank
else if (user.length()==0 &&pass.equals(pass)){
JOptionPane.showMessageDialog(null, "Fill in the empty field!");
}//check if user field is blank
else if(user.equals(user)&&pass!=conPass){
JOptionPane.showMessageDialog(null, "Password do not match!");
}//check if password and confirm pass matches
}
I dont really know how to say the problem but look in the if and else if statements, if the user meet one the those conditions, the program should print the JOptionPane thing. Except for the second else if.
You might be wondering why I put these codes at my else if
else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
getQuery(sqlLogin);
JOptionPane.showMessageDialog(null, "Account Successfully Created!");
create.dispose();
The reason for this is that, my program is having some logic error when I try to put it in if statement. Please help me with my code thanks :) Feel free to write a new code for me :DD
i might try something like this:
public static boolean isSet(String s){
if(s==null || "".equals(s)) return false;
return true;
}
//.... your validation here
if(isSet(user) && isSet(pass) && isSet(conPass) && pass.equals(conPass)){
//create account
}else{
//smth wrong eg. if(!pass.equals(conPass) { //wrongpass }
}

comparing jdatechooser with a jtextfield

Want to ask how can i compare a textfield with a JDateChooser?
if ((JTextField)jDateChooser1.getDateEditor().getUiComponent()).getText() < txtdate) {
JOptionPane.showMessageDialog(null, "Cant choose a date that is more previous >than the present date!");
} else {
JOptionPane.showMessageDialog(null, "Book Successfully Borrowed!");
}

Password as asterisk (*) [duplicate]

This question already has an answer here:
word (password) displays in asterisks - Console application
(1 answer)
Closed 9 years ago.
my password program can hide keyboard input . its invisible when i type password.
But my qus is how can show password input as asterisk (*)
can any one help me please? :p
my code is here:
public void login()
{
Scanner input = new Scanner(System.in);//create Scanner object
System.out.println("\n");
Console console = System.console();
char[] ad_password = "admin".toCharArray();
char[] sm_password = "salesman".toCharArray();
char[] passwordEntered = console.readPassword("Enter Password To Access The Project ( As Admin / Sales Man ): ");
if (Arrays.equals(ad_password, passwordEntered))
{
System.out.println("\n Congratulation!!! Access granted \n");
System.out.println("\n Welcome Admin. :-) \n\n");
Shoping_mall obj_ad_dis=new Shoping_mall();
obj_ad_dis.ad_dis();
}
else if(Arrays.equals(sm_password, passwordEntered))
{
System.out.println("\n Congratulation!!! Access granted \n");
System.out.println("\n Welcome Sales Man. :-) \n\n");
Shoping_mall obj1_sm_dis=new Shoping_mall();
obj1_sm_dis.sm_dis();
}
else
{
System.out.println("\n Error: Your Password Doesn't Meet. Access Denied !!! :-( \n\n");
System.out.println("\n Enter Correct Password.\n\n");
Shoping_mall obj_login=new Shoping_mall();//creat object for calling Admin_works() method
obj_login.login();
}
}
You can use JPasswordField to do that; Below is an example:
private JPasswordField password;
private String typedPassword;
private final String defaultPassword = "yourDesiredPassword";
public void createPasswordField(){
password = new JPasswordField(30);
password.setBounds(280, 240, 90, 20);
password.setEchoChar('*');
password.setBackground(Color.white);
password.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
password = (JPasswordField) e.getSource();
char [] tempPass = password.getPassword();
typedPassword = new String(tempPass);
if (!typedPassword.equals(defaultPassword)){
JOptionPane.showMessageDialog(null,
"Your password is not correct",
"Stack Over Flow example",
JOptionPane.ERROR_MESSAGE);
}
}
});
}

Condition Error

i am trying to have a check on that if value is null then don't show the message and recall the constructor, i did the following way but its not working.
if (title == null) {
JOptionPane.showMessageDialog(null, "Please Enter All Values");
new InfoFrame();
}
else {
try {
System.out.println(title+""+date);
System.out.println(title+""+date);
s.execute("INSERT INTO task ([title],[deadline],[priority],[time]) VALUES ('"+ title+ "','"+ date+ "','"+ priority + "','"+ time + "')");
JOptionPane.showMessageDialog(null,"Your Task has been added to the Database:");
} catch (Exception e) {
System.out.println(e.getMessage());
}
*Edited the var Title like stupid naming conventions
if (Title.isEmpty()) {
Will do the trick.
If you want to check both null or empty
if (Title == null || Title.isEmpty()) {
Also its better to start your variable in simple letters.

Categories

Resources