Results 1 to 3 of 3
Thread: check for same username
- 06-22-2011, 08:47 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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
- 06-22-2011, 09:25 PM #2
What ideas do you have for how to do this?code is creating a username but is not checking if the username exists
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
- 06-23-2011, 12:44 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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
-
Username And Password
By razmyasdfg in forum JDBCReplies: 9Last Post: 05-18-2011, 02:59 PM -
SELECT * FROM bla WHERE username IN (?, ?, ...) using PreparedStatement
By flok in forum JDBCReplies: 6Last Post: 03-04-2010, 12:16 PM -
Asking for a username with conditions
By ScentOfAWookie in forum New To JavaReplies: 2Last Post: 03-20-2009, 05:53 AM -
username password verification
By bheezee in forum JDBCReplies: 0Last Post: 11-25-2008, 06:55 PM -
JTextFields with username & password.
By Eric in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks