所以我正在创建一个名为 RegisterandLogin 的程序,基本上你必须输入用户名、密码和重新输入密码才能创建一个新帐户。可以使用以下方法验证用户和密码:
1.您在注册时输入的用户名不能与username.txt中的5个用户名中的任何一个相同。
2.另外用户名必须在5到8个字符之间,不带任何数字(即用户名只能是[AZ]或[az],但不能是[0-9]等任何数字)。
3.密码也是 5 到 8 个字符,允许使用数字(所以 12345 和 abcde 都是有效密码)。
4.重新输入的密码必须与密码相同,否则点击注册按钮后会输出错误信息。
当用户名通过 5 到 8 个字符的有效性检查,用户名在 username.txt 中不存在,并且密码通过有效性检查时,新的用户名被添加到 username.txt 中。
所以我已经用五个名字创建了 username.txt。但是,我遇到的问题是我不确定如何读取 username.txt 文件,以便它可以检查用户名是否已被使用。
这里的代码,有点长,任何帮助将不胜感激。
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.util.Scanner;
public class RegisterandLogin extends JFrame {
// variables for Sign up screen
private JPanel suP;
private JLabel suName, suPW, suRetypePW;
private JTextField suNameText, suPWText, suRetypePWText;
private JButton suButton;
public RegisterandLogin ()
{
super ("Register and Login");
signupScreen();
}
public void signupScreen () {
suP = new JPanel ();
// suP.setSize(50, 60);
setLayout (new FlowLayout());
setContentPane (suP);
suName = new JLabel ("Name");
suNameText = new JTextField (10);
suPW = new JLabel ("Password");
suPWText = new JTextField (10);
suRetypePW = new JLabel ("Retype Password");
suRetypePWText = new JTextField (10);
suButton = new JButton ("Register");
suP.add (suName);
suP.add(suNameText);
suP.add(suPW);
suP.add (suPWText);
suP.add (suRetypePW);
suP.add (suRetypePWText);
suP.add(suButton);
suP.setVisible(true);
ButtonHandler handlersu = new ButtonHandler();
suButton.addActionListener(handlersu);
}
public void validatebyarray() {
String[] read1=null;
String[] read=null;
read1=files(read);
int minL = 5, maxL = 8;
String stName = suNameText.getText();//username
String stPW = suPWText.getText();//password
String stRePW = suRetypePWText.getText();//retype password
/******************************************************
* Validate user name *
******************************************************/
if(stName.length()< minL || stName.length() > maxL ) // Check username length
System.out.println ("User name must be between 5 and 8");
else
{
//check invalid characters in username
for (int i = 0 ; i < stName.length(); i ++) // Check for invalid character
if (!
((stName.charAt (i)>= 'A' && stName.charAt (i) <= 'Z') ||
(stName.charAt (i)>= 'a' && stName.charAt (i) <= 'z')))
{
System.out.println ("Username contains invalid character");
break;
}
// Match the names in the array (file or database)
// Note the logic below works but is NOT secure since it discloses
// information about user names.
boolean uNfound = false;
for (int j = 0; j < 4; j++)
if (read1[j].equals(stName))
{
System.out.println ("User name " + read1[j] + " already exits");
uNfound = true;
break;
}
//if (!uNfound)
// System.out.println ("User name " + stName + " created");
}
System.out.println ("After UN");
/******************************************************
* Validate password *
******************************************************/
if(stPW.length()< minL || stPW.length() > maxL ) // Check username length
System.out.println ("Password must be between 5 and 8");
else
{
//check invalid characters in username
for (int i = 0 ; i < stPW.length(); i ++) // Check for invalid character
if (!
((stPW.charAt (i)>= '0' && stPW.charAt (i) <= '9') ||
(stPW.charAt (i)>= 'A' && stPW.charAt (i) <= 'Z') ||
(stPW.charAt (i)>= 'a' && stPW.charAt (i) <= 'z')))
{
System.out.println ("Password contains invalid character");
break;
}
// Note stName replaced by stPW and stRePW;
// uN[] replaced by pN[]
boolean uNfound = false;
if (stPW.equals(stRePW))
{
System.out.println ("User name " + stName + " created");
uNfound = true;
}
if (!uNfound)
System.out.println ("Passwords does not match");
}
//System.out.println ("After UN again");
}
class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event){
if (event.getSource () == suButton){
System.out.println ("SU button in register screen hit");
validatebyarray();
}
}
}
public String[] files(String [] read)
{
try {
BufferedReader file1 = new BufferedReader(new FileReader("usernames.txt"));
// Scanner fileReaderScan=new Scanner(readTextFile);
//only want first 5 lines
for(int count=0;count<5;count++)
{
read[count] = file1.readLine(); //use readLine method
System.out.println(read[count]);
}
file1.close(); //close file
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("OOps");
}
return read;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RegisterandLogin rl = new RegisterandLogin ();
rl.setSize(200, 300);
rl.setVisible(true);
rl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}