0

I am unable to return both username and pass String values at the same time.

    public static String executeLogin(String username, String pass) {
        int totalAttempts = 5;

        if (totalAttempts != 0) {
            if (username == "Javaprogram" && (pass == "Legend1@")) {
                System.out.println("Correct");
            } else {
                System.out.println("Incorrect Login");
                totalAttempts--;
            }
            if (totalAttempts == 0) {
                System.out.println("Maximum number of attempts exceeded");  
            }

        }
        return "username,pass";                 
    }
6
  • 6
    Unrelated to your question, that's not how you compare strings in java. Commented Oct 21, 2019 at 17:59
  • You cannot return multiple values with a single method (unless the method is called multiple times). Commented Oct 21, 2019 at 18:01
  • 3
    That's not possible with java. Create an object instead (let's say Credentials) that holds both values and return an instance of said object. Commented Oct 21, 2019 at 18:01
  • 4
    Also I can't think of any logical reason you would want to return username and pass...you are passing them into the method... thus you already have them in the caller? Commented Oct 21, 2019 at 18:05
  • a subclass of Object or an array[] Commented Oct 21, 2019 at 18:39

1 Answer 1

0

Note: you should have your 'total attempts' variable outside the scope of the function!

You can return a list of strings:

static int totalAttempts = 5;

public static String[] executeLogin(String username, String pass) {

    if (totalAttempts != 0) {
        if (username == "Javaprogram" && (pass == "Legend1@")) {
            System.out.println("Correct");
        } else {
            System.out.println("Incorrect Login");
            totalAttempts--;
        }
        if (totalAttempts == 0) {
            System.out.println("Maximum number of attempts exceeded");
        }

    }

    return new String[]{username, pass};
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why not just make a Credentials class and go the OOP way. Otherwise the user has to deal with how big the array actually is, maybe its empty or just one or three entries, who knows. It is just error prone.
i am trying to pass username and password from main method to the executelogin method.How to do that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.