Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java Check whether username already exists or not

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.24k
    Answer it

    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:

     

    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");
    }
    
    }
    }

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: