0

I've been making a basic java applet where it would input a username and a password on the first input then make me guess the username and password on the second input but it would only allow the user to have 3 tries on input then if the user fails to enter the right input it would become grayed out and non-editable I've so far managed to do most of it except the part where I have to limit the user to 3 input tries and the part on how to store the input data. TL;DR I need to know how to limit the number of tries by the user.

Here is the code that I've managed to make

import java.applet.Applet;           // import Applet class
import java.awt.*;              // (Abstract Windowing Toolkit)
import java.awt.event.*;
import javax.swing.*;
public class SetA extends Applet implements ActionListener
{
    int x = 0;
    // User Input
    Label lblinputuser = new Label("Input Username");
    TextField txtuserinput = new TextField(20);
    // Password Input
    Label lblinputpass = new Label("Input Password");
    TextField txtpassinput = new TextField(20);
    // User Guess
    Label lbluser = new Label("Username");
    TextField txtuser = new TextField(20);
    // Password Guess
    Label lblpass = new Label("Password");
    TextField txtpass = new TextField(20);
    // Button Confirmation
    Button btnOk = new Button("Ok");
    Button btnOk2 = new Button("Ok");

    public void init()
    {
        add(lblinputuser);
        add(txtuserinput);
        add(lblinputpass);
        add(txtpassinput);
        add(btnOk2);
        add(lbluser);
        add(txtuser);
        add(lblpass);
        add(txtpass);
        add(btnOk);

        txtuserinput.setForeground(Color.RED);
        txtuserinput.setBackground(Color.BLACK);
        txtpassinput.setForeground(Color.RED);
        txtpassinput.setBackground(Color.BLACK);
        txtuserinput.setEchoChar('*');
        txtpassinput.setEchoChar('*');
        btnOk2.addActionListener(this);
        btnOk.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == btnOk2)
        {
            int confirmOption = JOptionPane.showConfirmDialog(null, "Are you sure?","Message",JOptionPane.YES_NO_OPTION);
            if(confirmOption == 1)
            {
                txtuserinput.setEditable(false);
                txtpassinput.setEditable(false);
            }
        }
        if (e.getSource() == btnOk)
        {

            if(x != 3)
            {  
                
                if(txtuser.getText() != (txtuserinput.getText()) && txtpass.getText() != (txtpassinput.getText()))
                {
                    x = x++;
                    JOptionPane.showMessageDialog(null,"Error Password/Username Incorrect","Error",JOptionPane.ERROR_MESSAGE);
                }

                if(x == 3)
                {
                    JOptionPane.showMessageDialog(null,"You've run out of tries. Program Closing","Error",JOptionPane.ERROR_MESSAGE);
                    txtuser.setEditable(false);
                    txtpass.setEditable(false);
                    txtuser.setEchoChar('*');
                    txtpass.setEchoChar('*');
                }
                else
                { 
                    JOptionPane.showMessageDialog(null,"You've guessed the right input","Congratulations",JOptionPane.PLAIN_MESSAGE);
                }
            }
        }
    }
}
  • Keep a `int counter` variable as an instance field, increment it on error, check its value before allowing to continue. – Federico klez Culloca Jan 21 '21 at 10:31
  • Also, `txtuser != txtuserinput` [this is not how you compare strings](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). And you're comparing the `TextField`s themselves, not their content. – Federico klez Culloca Jan 21 '21 at 10:32
  • replaying for @FedericoklezCulloca comment: use .getText(); method –  Jan 21 '21 at 10:37
  • Unclear what version of Java you are using, but applets were removed from Java 11 and Java 8 should be considered end-of-life – OneCricketeer Feb 12 '21 at 19:29

1 Answers1

0

This is the needed logic:

  1. Initialize a counter.
  2. In actionPerformed, increase the counter.
  3. Add additional if-else condition to check whether the counter is <= 3

Done.

  • I've updated the code and tried several ways to make it increment the counter per press of the Ok when the input is wrong but It still won't show the part where it reaches the maximum tries – Keneth Cayas Jan 22 '21 at 09:17