-2

I tried to write a program that lets you choose if you want to register or login and if chose author wise it will let you know that you can only choose register or login... I have a problem that the program sometimes stuck in the loop which tells me that I have entered the wrong input although I type "login" and you cannot get out of this loop. one more problem I got is the login code: I added user_id who is equal to the user count+1 and I wanted to check if the user and pass that I got in the login are both correct with a for loop which loops over the number of users and check if the user input is equal to the password of every user with the user_id and I just don't know how to do it I thought maybe to give the object of any user with the count and so I could check user one by one in my for loop user_id.username and user_id.password.

import java.util.Scanner;

public class users {
    public String user_name;
    public int user_id = 1;
    private String password;
    public static int count = 1;
    public static String input;

    public users(String Ruser, String Rpassword) {

        this.user_id = count++;
        this.user_name = Ruser;
        this.password = Rpassword;
        count++;

        System.out.printf("User %s has been crated \n", Ruser);
        System.out.printf("Enter 'login' to log in or 'register' to open another account");

    }

    public static void login(String Luser, String Lpassword) {
        for (int i = 1; i <= count; i++) {
            System.out.printf("Enter 'login' to log in or 'register' to open another account");
            // user_id.users
            // if(this.user_name)
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("login");
        System.out.println("register");
        input = scanner.nextLine();


            while (input.equals("login")) {

                System.out.println("username");
                String Luser = scanner.nextLine();
                System.out.println("Password");
                String Lpassword = scanner.nextLine();
                int a = count;
                login(Luser, Lpassword);
                System.out.println("");
                input = scanner.nextLine();
            }
            while (input.equals("register")) {

                System.out.println("username");
                String Ruser = scanner.nextLine();
                System.out.println("Password");
                String Rpassword = scanner.nextLine();
                users count = new users(Ruser, Rpassword);
                System.out.println("");
                input = scanner.nextLine();
            }
            while ((!input.equals("register")) || (!input.equals("login"))) {
                System.out.println("invild option, chose login or regiser!");
                input = scanner.nextLine();


        } 
Andrew Naguib
  • 3,978
  • 2
  • 21
  • 42
YT ef
  • 1
  • 2
  • The reason why you're stuck in a loop is because the moment you type in either 'register' or 'login' you actovate the while loop and there is no breaking condition for it, therefore it keeps on going forever. Try something much simpler like an if statement. – f78xd23 Dec 28 '17 at 00:33
  • Try this question as the first step in fixing your code. https://stackoverflow.com/questions/5032356/using-scanner-nextline – f78xd23 Dec 28 '17 at 00:44

1 Answers1

0

In the main method I would do something like this:

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("login");
    System.out.println("register");


    while(true) {
        input = scanner.nextLine();
        switch(input) {
        case "exit":
            System.exit(0);
            break;
        case "login":
            System.out.println("username");
            String Luser = scanner.nextLine();
            System.out.println("Password");
            String Lpassword = scanner.nextLine();
            int a = count;
            login(Luser, Lpassword);
            System.out.println("");
            //              input = scanner.nextLine();
            break;
        case "register":
            System.out.println("username");
            String Ruser = scanner.nextLine();
            System.out.println("Password");
            String Rpassword = scanner.nextLine();
            Users count = new Users(Ruser, Rpassword);
            System.out.println("");
            //              input = scanner.nextLine();
            break;
        default:
            System.out.println("invild option, chose login, regiser or exit!");
            continue;
        }
    }