Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I created a program where the user will type in a password and username. The program will take the text file where this information is saved and convert it into an arraylist. Once the information is validated, that username and password will be removed from the arraylist. The user shouldn't be able to sign in again, unless the program is restarted.

My issue is that the users information is never removed from the arraylist.

Here is my code:
import java.io.*;
import java.util.*;
public class LoginPagev2
{
	public static void main(String[] args) throws IOException
	{
		String answ;
		String user;
		String pass ;
		int counter = 0;
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Welcome! This program will allow you to sign in with a username and password you created.");
		System.out.println("If you fail to login correctly 3 times, this program will terminate. You can only sign in once per username.");
		System.out.println("If you wish to sign in into the same account twice, you must restart the program.");
		while(true)
		{	System.out.print("Would you like to sign in? (Y/N): ");
			answ = keyboard.nextLine();
			if(answ.equalsIgnoreCase("N"))
			{
				System.out.print("Goodbye!");
				System.exit(0);
			}
			System.out.print("What is your username?: " );
			user = keyboard.nextLine();
			System.out.print("What is your password?: ");
			pass = keyboard.nextLine();
			while((!list().contains(user) || !list().contains(pass)) || counter < 3)
			{
				++ counter;
				if(list().contains(user) && list().contains(pass))
				{
						list().remove(user);
						list().remove(pass);
						System.out.println("Congrats! You successfully signed in!");
						counter = 0;
						break;
				}
				if(counter==3)
				{
					System.out.println("You have attempted you max amount of tries. This program will now end.");
					System.exit(0);
				}
				else
				{
					System.out.println("This is not valid. Please try again");
					System.out.print("What is your username?: " );
					user = keyboard.nextLine();
					System.out.print("What is your password?: ");
					pass = keyboard.nextLine();
				}
			}
			System.out.print("Would you like to sign in with another account? (Y/N): ");
			String rep = keyboard.nextLine();
			if(rep.equalsIgnoreCase("N"))
			{
				System.out.print("Goodbye!");
				System.exit(0);
			}
			else
				continue;
		}
		
	}			
	
	public static ArrayList<String> list() throws IOException
	{
		try
		{
			BufferedReader reader = new BufferedReader(new FileReader("loginInfo.txt"));
			String line;
			ArrayList<String> arr = new ArrayList<String> ();
			while ((line = reader.readLine()) != null) 
			{
				arr.add(line);
			}
			reader.close();
			return arr;
		}catch(Exception e)
		{
			return null;
		}
	}
	
}


What I have tried:

I'm not really sure how to fix this issue, so I didn't really try anything.
Posted
Updated 11-Aug-19 18:56pm

It’s because you’re re-instantiating the list each time you call the list method.
 
Share this answer
 
Each time you call list you create a new ArrayList and add the user info to it:
Java
	public static ArrayList<String> list() throws IOException
	{
...
			ArrayList<String> arr = new ArrayList<String> ();
			while ((line = reader.readLine()) != null) 
			{
				arr.add(line);
			}
			reader.close();
			return arr;
...
	}
So each time you use it, you get one user's information and then throw it away!
As a result, the user is never in the collection next time you try to find it!

You need to do one of two things:
1) Create an ArrayList at class level (outside any method) and use that instead of creating a new one in list
Or
2) Create an ArrayList in main and pass it to list each time you call it.

But ... you are really doing this very, very wrong.
By keeping usernames and passwords in the same collection, you can';t tell if I'm logging in as my password with by username, or logging in as my username with your password! Because your search will find a match if it locates any string: if I create a user "Griff" with password "Original", and your do the same with "Noney" and "Abees", Anyone can log in with any combination:
Griff    Original
Griff    Griff
Griff    Noney
Griff    Abees
Original Original
Original Griff
Original Noney
Original Abees
Noney    Original
Noney    Griff
Noney    Noney
Noney    Abees
Abees    Original
Abees    Griff
Abees    Noney
Abees    Abees
All would be valid combinations for a successful login.
What you need to do if create either separate collections for names and passwords and compare the password only with the same index as the required username; or better create a User class which contains the username and password, then search for that in your collection instead of the "raw strings".
 
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