The code doesn't work!



  • Hey! I'm studying Java just recently. The challenge is that a person should introduce a name, a logic and password into the consoles, after which the programme should check the logic involvement in the dynamic area and, if he is not, add it and, if he is, make a mistake warning.
    The opening opens a window of entry, where the above-mentioned name, login and passwords are freely introduced. However, the programme does not verify the logic of involvement and fails to erroneous the message, nor does it remove the area itself. I'll be grateful for your help!

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    

    public class User {

    public static void main(String[] args) throws IOException {
        BufferedReader bReader =new BufferedReader(new InputStreamReader(System.in));
        String name = bReader.readLine();
        String login = bReader.readLine();
        String password = bReader.readLine();
        UserADD(name,login,password);
    }
    
    public static ArrayList<String> UserADD(String name,String login,String password) {
        ArrayList <String> users = new ArrayList<String>();
        for (int i = 0; i<users.size(); i++) {
            if (users.contains(login)) {
                System.out.println("Это имя пользователя уже занято");
            }
            else users.add(name);   users.add (login);users.add(password);
        {
            System.out.println(users);
        }
    
        }
        return users;
    }
    

    }


    Thank you very much, but the problem remains. In an attempt by the second user to introduce the logic of the first, the programme accepts it calmly, ignoring any restrictions and a warning on the screen. I tried all the options.



  • First of all, a very strange section of the code.

    else users.add(name);   users.add (login);users.add(password);
    {
        System.out.println(users);
    }
    

    It looks like you're in the brackets.

    Secondly, the challenge is to check the logic for employment. I don't see the point of adding a name and password to the list.

    Thirdly, you don't need a reset. Enough.

    if(users.contains(login)) {
        System.out.println("Это имя пользователя уже занято");
    }
    else {
        users.add(login);
    }
    

    Finally, you create an empty list on every challenge to the userADD method and you don't keep it anywhere. When the method is completed, variable users leave the visibility area and the garbage collector removes it.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2