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";                 
    }
Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
  • 6
    Unrelated to your question, that's not how you [compare strings in java](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – azurefrog Oct 21 '19 at 17:59
  • You cannot return multiple values with a single method (unless the method is called multiple times). –  Oct 21 '19 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. – jBuchholz Oct 21 '19 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? – RobOhRob Oct 21 '19 at 18:05
  • a subclass of Object or an array[] – Marcos Vasconcelos Oct 21 '19 at 18:39
  • Go the OOP route. Create a `Credentials` class which has a `String name;` and `String password` field. Add a constructor, some nice getters etc and then use this class instead. – Zabuzard Oct 21 '19 at 18:53

1 Answers1

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};
}
Samurott
  • 26
  • 2
  • 1
    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. – Zabuzard Oct 21 '19 at 18:51
  • i am trying to pass username and password from main method to the executelogin method.How to do that? – Prathamesh Shirshivkar Oct 22 '19 at 11:27