Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having a file called customersinfo.txt this is how my file structure looks like
ID FirstName LastName Email Mobile UserName PassWord ConPassWord 1 John Sam JS@gmail.com 0123 JhonSam12 1234 1234 2 ---- ------ ----------- ---- ----- ------ -------


So my problem is it accepts multiple users with the same username what I want is to check when the customer enters his username whether if that username already exists in the file or not if that username already exists try to enter a different username if not accept and save it to the file

What I have tried:

Java
package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.*;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Read
{
    public void display ()
    {
        String filepath = "customersAllInfo.txt";
        String editTerm = "343";
        String newFirstName = "1";
        String newLastName = "1";
        String newEmailID = "1";
        String newMobile = "1";
        String newPassword = "1";
        String newPasswordCon = "1";

        CreatNewUser(filepath,newFirstName,newLastName,newEmailID,newMobile,editTerm,newPassword,newPasswordCon);
    }

    private static Scanner x;

    public void CreatNewUser (String filepath,String newFirstName,String newLastName,String newEmailID,String newMobile,String editTerm,String newPassword,String newPasswordCon)
    {
        boolean found = false;
        File newCustomer = new File("customersAllInfo.txt");
        File oldFile = new File(filepath);
        String IDF1 = ""; String saveFname1 = ""; String saveLname1 = ""; String saveEmail1 = ""; String saveMobile1 = ""; String saveUser1 = "";
        String saveFpass1 = ""; String saveLpass1 = "";
        try
        {
            x = new Scanner(new File(filepath));
            x.useDelimiter("[,\n]");

            while (x.hasNext() && !found)
            {
                IDF1        = x.next();
                saveFname1  = x.next();
                saveLname1  = x.next();
                saveEmail1  = x.next();
                saveMobile1 = x.next();
                saveUser1   = x.next();
                saveFpass1  = x.next();
                saveLpass1  = x.next();

                if (saveUser1.trim().equals(editTerm.trim()))
                {
                    //found = true;
                    System.out.println("Already Exists");
                    found = true;
                }

                else
                {
                    newCustomer = new File("customersAllInfo.txt");
                    try
                    {
                        newCustomer.createNewFile();
                    }
                    catch (IOException ex)
                    {
                        System.out.println("customersAllInfo FILE COULDN'T BE CREATED");
                    }

                    try
                    {
                        BufferedWriter a = new BufferedWriter(new FileWriter(newCustomer,true));
                        a.write( newFirstName + "," + newLastName + "," + newEmailID + "," + newMobile + editTerm + "," + newMobile + "," + newPassword + "," + newPasswordCon + "\n");
                        a.close();
                        System.out.println("DONE");
                    }
                    catch (IOException ex)
                    {
                        System.out.println("ERROR IN ENTERING THE INFORMATION");
                    }

                }
                }
            x.close();
        }
        catch (Exception e)
        {
            System.out.println("ERROR ERROR TO FIND FILE");
        }

    }
}
Posted
Updated 18-Mar-20 7:26am
v2
Comments
Richard MacCutchan 18-Mar-20 8:30am    
Why not use a database rather than a text file? It makes it easy to check for duplicates.
yusof26 18-Mar-20 9:58am    
I do agree with you but it is an assignment and database is not allowed
Richard MacCutchan 18-Mar-20 12:10pm    
OK, that's fine.

That means that you will need to read the entire file each time. As you read the file, create a list (see the Java collection types) that maps the username to the remaining details. You can then do a quick lookup on the list to see if a username already exists. If you cannot use a collection class then you can still use a simple array, although you will need to write all the search code for yourself.
yusof26 18-Mar-20 13:10pm    
Thanks a lot for the great tip
Richard MacCutchan 18-Mar-20 13:24pm    
You are welcome.

1 solution

The simplest way is to add a second file (or format your file so it contains an extra table)
This second file is an index file, and it contains two bits of info:
A hashcode - the username, hashed with MD5 or SHA (MD5 is officially broken, but it's fine for non-security applications like this)
An index - the position in the original file at which the user record begins.

Read the index file into memory - it's not massive - and when you need to find or check a username, you hash it, find the hash in your memory index table. If it's there, compare the actual strings - hash collisions are rare, but they do happen - it they are the same, it's the same username. It's probably even acceptable just to reject a username if the hash is a collision!

The advantage of this approach is that all the records in the index file are the same size: the hash size plus an integer index - so it's trivial to read, and trivial to compare, unlike variable length usernames and rows - and hashing is pretty quick.
 
Share this answer
 
Comments
yusof26 18-Mar-20 8:04am    
@ OriginalGriff
First thanks a lot for spending some time to write your answer. Second I want to say that I've got what you mean if there is another way and much simpler or maybe you can write a sample code that explains your answer I would appreciate it.

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