Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I use the below code to implement an unsuccesful login attempt in a logi

ID: 3702127 • Letter: H

Question

How can I use the below code to implement an unsuccesful login attempt in a login application which is also below. Can you incorporate into the logon application?

Unsuccesful Login Attempts

int counter = 0;

            int i = 0;

            // for (int i = 0; i <= 3; i++) {

            while (i < 3) {

                String au = ausernametxt.getText();

                String ap = apasswordtxt.getText();

    

                String pass = "111";

                String user = "zsa";

    

                if (user.equals(ausernametxt.getText()) && (pass.equals(apasswordtxt.getText()))) {

                    JOptionPane.showMessageDialog(null, "Successfully log in!");

                    new menu().setVisible(true);

                 return;

                }

               

               

    

                if (user != (ausernametxt.getText()) && (pass.equals(apasswordtxt.getText()))) {

                    JOptionPane.showMessageDialog(null, "Invalid User No", "LOG IN", JOptionPane.ERROR_MESSAGE);

                    counter += 1;

                    System.out.println(counter);

    

                }

    

                else if (user.equals(ausernametxt.getText()) && (pass != (apasswordtxt.getText()))) {

                    JOptionPane.showMessageDialog(null, "Invalid Password", "LOG IN", JOptionPane.ERROR_MESSAGE);

                    counter++;

Login Application

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Login;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.lang.*;
import java.util.concurrent.TimeUnit;
import java.util.Random;

public class Login extends Application {
  
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(" Login");
        // Grid Pane divides your window into grids
        GridPane grid = new GridPane();
        // Align to Center
        // Note Position is geometric object for alignment
        grid.setAlignment(Pos.CENTER);
        // Set gap between the components
        // Larger numbers mean bigger spaces
        grid.setHgap(10);
        grid.setVgap(10);

        // Create some text to place in the scene
        Text scenetitle = new Text("Welcome. Login to continue.");
        // Add text to grid 0,0 span 2 columns, 1 row
        grid.add(scenetitle, 0, 0, 2, 1);

        // Create Label
        Label userName = new Label("User Name:");
        // Add label to grid 0,1
        grid.add(userName, 0, 1);

        // Create Textfield
        TextField userTextField = new TextField();
        // Add textfield to grid 1,1
        grid.add(userTextField, 1, 1);

        // Create Label
        Label pw = new Label("Password:");
        // Add label to grid 0,2
        grid.add(pw, 0, 2);

        // Create Passwordfield
        PasswordField pwBox = new PasswordField();
        // Add Password field to grid 1,2
        grid.add(pwBox, 1, 2);

        // Create Login Button
        Button btn = new Button("Login");
        // Add button to grid 1,4
        grid.add(btn, 1, 4);

        final Text actiontarget = new Text();
        grid.add(actiontarget, 1, 6);

        // Set the Action when button is clicked
        btn.setOnAction(new EventHandler<ActionEvent>() {
                @Override
            public void handle(ActionEvent e) {
                // Authenticate the user
                boolean isValid = authenticate(userTextField.getText(), pwBox.getText());
                // If valid clear the grid and Welcome the user
                if (isValid) {
                    grid.setVisible(false);
                    GridPane grid2 = new GridPane();
                    // Align to Center
                    // Note Position is geometric object for alignment
                    grid2.setAlignment(Pos.CENTER);
                     // Set gap between the components
                    // Larger numbers mean bigger spaces
                    grid2.setHgap(10);
                    grid2.setVgap(10);
                    Text scenetitle = new Text("Welcome " + userTextField.getText() + "!");
                    // Add text to grid 0,0 span 2 columns, 1 row
                    grid2.add(scenetitle, 0, 0, 2, 1);
                    Scene scene = new Scene(grid2, 500, 400);
                    primaryStage.setScene(scene);
                    primaryStage.show();
                   // If Invalid Ask user to try again
                } else {                  
                    final Text actiontarget = new Text();
                    grid.add(actiontarget, 1, 6);
                    actiontarget.setFill(Color.FIREBRICK);
                    actiontarget.setText("Please try again.");
                }

            }
        });
        // Set the size of Scene
        Scene scene = new Scene(grid, 500, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    //this will generate the otp can send to mobile no and use it for two
    //step verification and can verify the solution
    public static String generateOTP() {
        Random rand = new Random();
        Integer code = rand.nextInt(999999
                - 100000+ 1) + 100000;
        return code.toString();
           }
    public static void main(String[] args) {
        launch(args);
    }

    /**
     * @param user the username entered
     * @param pword the password entered
     * @return isValid true for authenticated
     */
    public boolean authenticate(String user, String pword) {
        boolean isValid = false;
        if (user.equalsIgnoreCase("sdevadmin")
                && pword.equals("425!pass")) {
            isValid = true;
        }

        return isValid;
    }

}

Explanation / Answer

Here is your solution:

import java.util.Random;

import javax.swing.JOptionPane;

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.PasswordField;

import javafx.scene.control.TextField;

import javafx.scene.layout.GridPane;

import javafx.scene.paint.Color;

import javafx.scene.text.Text;

import javafx.stage.Stage;

public class Login extends Application {

int counter = 0;

@Override

public void start(Stage primaryStage) {

primaryStage.setTitle(" Login");

// Grid Pane divides your window into grids

GridPane grid = new GridPane();

// Align to Center

// Note Position is geometric object for alignment

grid.setAlignment(Pos.CENTER);

// Set gap between the components

// Larger numbers mean bigger spaces

grid.setHgap(10);

grid.setVgap(10);

// Create some text to place in the scene

Text scenetitle = new Text("Welcome. Login to continue.");

// Add text to grid 0,0 span 2 columns, 1 row

grid.add(scenetitle, 0, 0, 2, 1);

// Create Label

Label userName = new Label("User Name:");

// Add label to grid 0,1

grid.add(userName, 0, 1);

// Create Textfield

TextField userTextField = new TextField();

// Add textfield to grid 1,1

grid.add(userTextField, 1, 1);

// Create Label

Label pw = new Label("Password:");

// Add label to grid 0,2

grid.add(pw, 0, 2);

// Create Passwordfield

PasswordField pwBox = new PasswordField();

// Add Password field to grid 1,2

grid.add(pwBox, 1, 2);

// Create Login Button

Button btn = new Button("Login");

// Add button to grid 1,4

grid.add(btn, 1, 4);

final Text actiontarget = new Text();

grid.add(actiontarget, 1, 6);

// Set the Action when button is clicked

btn.setOnAction(new EventHandler<ActionEvent>() {

@Override

public void handle(ActionEvent e) {

// for (int i = 0; i <= 3; i++) {

if (counter < 3) {

String user = "zsa";

String pass = "111";

if (user.equals(userTextField.getText()) && pass.equals(pwBox.getText())) {

JOptionPane.showMessageDialog(null, "Successfully log in!");

grid.setVisible(false);

GridPane grid2 = new GridPane();

// Align to Center

// Note Position is geometric object for alignment

grid2.setAlignment(Pos.CENTER);

// Set gap between the components

// Larger numbers mean bigger spaces

grid2.setHgap(10);

grid2.setVgap(10);

Text scenetitle = new Text("Welcome " + userTextField.getText() + "!");

// Add text to grid 0,0 span 2 columns, 1 row

grid2.add(scenetitle, 0, 0, 2, 1);

Scene scene = new Scene(grid2, 500, 400);

primaryStage.setScene(scene);

primaryStage.show();

}

if (!user.equals(userTextField.getText()) && pass.equals(pwBox.getText())) {

JOptionPane.showMessageDialog(null, "Invalid User No", "LOG IN", JOptionPane.ERROR_MESSAGE);

counter += 1;

}

else if (user.equals(userTextField.getText()) && (!pass.equals(pwBox.getText()))) {

JOptionPane.showMessageDialog(null, "Invalid Password", "LOG IN", JOptionPane.ERROR_MESSAGE);

counter++;

} else if (!user.equals(userTextField.getText()) && !pass.equals(pwBox.getText())) {

JOptionPane.showMessageDialog(null, "Invalid Credentials", "LOG IN", JOptionPane.ERROR_MESSAGE);

counter += 1;

}

} else {

JOptionPane.showMessageDialog(null, "Unsuccessful Login Attempts","Attempts", JOptionPane.ERROR_MESSAGE);

}

}

});

// Set the size of Scene

Scene scene = new Scene(grid, 500, 400);

primaryStage.setScene(scene);

primaryStage.show();

}

// this will generate the otp can send to mobile no and use it for two

// step verification and can verify the solution

public static String generateOTP() {

Random rand = new Random();

Integer code = rand.nextInt(999999 - 100000 + 1) + 100000;

return code.toString();

}

public static void main(String[] args) {

launch(args);

}

/**

* @param ausernametxt

* @param apasswordtxt

* @return

*/

/* public boolean authenticate(TextField ausernametxt, TextField apasswordtxt) {

boolean isValid = false;

return isValid;

}*/

}

Note:- I've done some changes in your code to get the exact result. Hope you understand and i'm not using authenticate method. And i have removed while loop and placed it with if condition. I've taken counter as a public variable to increase the value for every unsuccessful login.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote