Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
 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();


        }
<pre>
import

What I have tried:

i tried messing with the code for hours..
Posted
Updated 9-Sep-20 18:24pm
v2

1 solution

Too many while loops, and the expression on the last one is wrong, it should be:
Java
// use && as both expressions need to be true.
            while ((!input.equals("register")) && (!input.equals("login"))) {
                System.out.println("invild option, chose login or regiser!");
                input = scanner.nextLine();

A better way would be a single do/while loop, something like:
Java
do
{
    System.out.println("Enter \"login\", \"register\", or \"exit\"");
    input = scanner.nextLine();
    if (input.equals("login")
    {
        // get login details
    }
    else if (input.equals("register")
    {
        // get register details
    }
    else if (input.equals("exit")
    {
        break; // exit the loop
    }
    else
    {
        // invalid input, tell them to try again
    }
} while (true);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900