Results 1 to 3 of 3
  1. #1
    markafar is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default check for same username

    hi guys, i need some help to solve a problem which i am encountering. basically i have designed a very simple login screen, the username is created and saved in a textfile. i need to first make sure that no similar username is there so i have to check the username is this exists in the text file. the code i have written is as follows.

    File destFile = new File("C:\\DATAX\\DESTINATIONS\\STAGING\\usernames. txt");
    try
    {
    String userNames = userTextField.getText();
    System.out.println(userNames);
    FileInputStream fstream = new FileInputStream(destFile);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader existingUsers = new BufferedReader(new InputStreamReader(in));
    String strLine;
    // System.out.println(strLine);
    while((strLine = existingUsers.readLine()) != null)
    {
    System.out.println(strLine);
    boolean exists = userNames.equals(strLine);
    if (exists == true)
    {
    JOptionPane.showMessageDialog(null, "User name already exists !! \n Please choose another username");
    break;
    }
    else
    {
    BufferedWriter out = new BufferedWriter(new FileWriter(destFile, true));
    userTextField.write(out);
    out.newLine();
    out.flush();
    out.close();
    }
    }
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    JOptionPane.showMessageDialog(null,"Username successfully created");
    }


    the code is creating a username but is not checking if the username exists. can someone help me pls?

    tks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    code is creating a username but is not checking if the username exists
    What ideas do you have for how to do this?
    You should save the username in a collection and have a lookup to see if the name is in the collection.
    Look at the HashSet class for one place to save the names.

    Please put your posted code in code tags to make it easier to read. See: BB Code List - Java Forums

  3. #3
    markafar is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default OK found the solution

    found the solution to my problem. this is how i solved it. thanks for your help.

    Java Code:
    File destFile = new File("C:\\DATAX\\DESTINATIONS\\usernames.txt");
            boolean exists = false;
            try {
                String userNames = destField.getText();
                FileInputStream fstream = null;
                DataInputStream in = null;
                BufferedReader existingUsers = null;
              try {
                    fstream = new FileInputStream(destFile);
                    in = new DataInputStream(fstream);
                    existingUsers = new BufferedReader(new InputStreamReader(in));
                    String strLine;
                    while ((strLine = existingUsers.readLine()) != null) {
                        exists = userNames.equals(strLine);
                        if (exists == true) {
                            break;
                        }
                        }
                       }
                finally {
                    existingUsers.close();
                    in.close();
                    fstream.close();
                }
                if (exists) {
                    JOptionPane.showMessageDialog(null, "This destination already exists !!\nPlease choose another destination name");
                } else {
                    BufferedWriter out = null;
                    try {
                        out = new BufferedWriter(new FileWriter(destFile, true));
                        destField.write(out);
                        out.newLine();
                        JOptionPane.showMessageDialog(null, "Username successfully created");
                    } finally {
                        out.flush();
                        out.close();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

Similar Threads

  1. Username And Password
    By razmyasdfg in forum JDBC
    Replies: 9
    Last Post: 05-18-2011, 02:59 PM
  2. Replies: 6
    Last Post: 03-04-2010, 12:16 PM
  3. Asking for a username with conditions
    By ScentOfAWookie in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 05:53 AM
  4. username password verification
    By bheezee in forum JDBC
    Replies: 0
    Last Post: 11-25-2008, 06:55 PM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-01-2007, 11:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •