所以我开始为我的PAT 12级任务创建这个项目,我遇到了一个减速带……我有一个登录屏幕,使用jFrame,在netbean中。我的问题是,我使用2个文本文件来存储数据:1个用于用户名,1个用于密码。它们使用扫描仪读取,并用#分隔。关键是,我使用同时循环验证用户名,但我无法验证与另一个文本文件中的用户名对应的密码。
所以你可以想象2个文本文件:
当点击“登录”按钮时,它使用while循环来检查用户名是否已经存在。如果是,继续输入密码,否则“用户名不存在”。当在另一个文本文件的相同位置匹配密码时,问题就出现了。
如果有人能帮助我,这将是我的一周,拯救我的项目。请理解,Netbeans只允许您编辑特定区域的代码。我尽最大努力让我的代码干净易懂。
非常感谢,雪花吸血鬼
package Battlefield4;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Login extends javax.swing.JFrame
{
String Username;
String Password;
public Login()
{
initComponents();
}
//Can not edit from here on
private void initComponents() {
txtUsername = new javax.swing.JTextField();
txtPassword = new javax.swing.JTextField();
lblPassword = new javax.swing.JLabel();
lblUsername = new javax.swing.JLabel();
btnLogin = new javax.swing.JButton();
btnCreate = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setSize(new java.awt.Dimension(854, 480));
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
getContentPane().add(txtUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 150, 210, -1));
getContentPane().add(txtPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 220, 210, -1));
lblPassword.setText("Password");
getContentPane().add(lblPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 200, -1, -1));
lblUsername.setText("Username");
getContentPane().add(lblUsername, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 130, -1, -1));
btnLogin.setText("Login");
btnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnLoginActionPerformed(evt);
}
});
getContentPane().add(btnLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 290, -1, -1));
btnCreate.setText("Create");
btnCreate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCreateActionPerformed(evt);
}
});
getContentPane().add(btnCreate, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 290, -1, -1));
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Battlefield4/Batfield/Background/Login (Small).jpg"))); // NOI18N
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 860, 480));
pack();
}
//Can not edit before this
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
//This is from where Netbeans lets you edit your code.
try {
File user = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Username.txt"); //To create a universal file for Input & Output
File pass = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Password.txt"); //To create a universal file for Input & Output
user.getParentFile().mkdirs(); //To denote the parent file
pass.getParentFile().mkdirs(); //To denote the parent file
Scanner scUser = new Scanner(user).useDelimiter("#"); //To scan the Username file
Scanner scPass = new Scanner(pass).useDelimiter("#");
Username = txtUsername.getText(); //This gets the user input
Password = txtPassword.getText(); //This gets the user input
int pos = 0; //Indicates the position of the Username in the save file
while(scUser.hasNext())
{
pos = pos + 1;
if(scUser.equals(Username))
{ //This is where it fails
for (int i = 0; i <= 5; i++)
{
while(scPass.hasNext())
{
System.out.print(scPass);
}
}
if(scPass.equals(Password))
{
new Selection().setVisible(true);
this.dispose();
}
else
{
JOptionPane.showMessageDialog(null,"Incorrect Password!");
}
}//This is where it works from. The above code is sketchy
else
{
JOptionPane.showMessageDialog(null,"User Does Not Exist!");
}
scUser.close();
}
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {
//Again this is from where Netbeans lets you edit
try {
File user = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Username.txt"); //To create a universal file for Input & Output
File pass = new File("C:\\Users\\John1012\\Documents\\NetBeansProjects\\PATProject\\src\\Battlefield4\\Batfield\\Users\\Password.txt"); //To create a universal file for Input & Output
user.getParentFile().mkdirs(); //To denote the parent file
pass.getParentFile().mkdirs(); //To denote the parent file
Scanner scUser = new Scanner(user); //To scan the Username file
Username = txtUsername.getText(); //This gets the user input
Password = txtPassword.getText(); //This gets the user input
if(scUser.nextLine().contains(Username)) //This reads the entire line and checks for an indexOf of those characters
{
JOptionPane.showMessageDialog(null,"User Already Exists!");
scUser.close(); //Close the Scanner
}
else
{
scUser.close(); //Close the Scanner
if("".equals(Password) || " ".equals(Password)) //Checks to see whether or not the user entered a password
{
JOptionPane.showMessageDialog(null,"Please Enter A Password!");
}
else
{
PrintWriter pwUser = new PrintWriter(new FileWriter(user,true)); //To Write to the Username file
PrintWriter pwPass = new PrintWriter(new FileWriter(pass,true)); //To Write to the Password file
pwUser.print(Username + "#");
pwPass.print(Password + "#");
pwUser.close();
pwPass.close();
new Selection().setVisible(true);
this.dispose();
}
}
scUser.close();
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
//Can no longer edit here
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnCreate;
private javax.swing.JButton btnLogin;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblPassword;
private javax.swing.JLabel lblUsername;
private javax.swing.JTextField txtPassword;
private javax.swing.JTextField txtUsername;
// End of variables declaration
}
所以我想出了怎么做。谢谢大家的帮助!
我把这个作为一个更简单的方法发布到@Tobi,希望能帮助那些不太先进或需要更简单帮助的人!
private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {
try {
File user = new File("src\\Battlefield4\\Batfield\\Users\\Username.txt"); //To create a universal file for Input & Output
File pass = new File("src\\Battlefield4\\Batfield\\Users\\Password.txt"); //To create a universal file for Input & Output
user.getParentFile().mkdirs(); //To denote the parent file
pass.getParentFile().mkdirs(); //To denote the parent file
Scanner scUser = new Scanner(user).useDelimiter("#"); //To scan the Username file
Scanner scPass = new Scanner(pass).useDelimiter("#"); //To scan the password file
username = txtUsername.getText(); //This gets the user input
password = txtPassword.getText(); //This gets the user input
int pos = 0; //Indicates the position of the Username in the save file
boolean loggedIn = false; //Flag to check if it's logged in
while(scUser.hasNext() && scPass.hasNext()) //Runs files in congruency
{
if(scUser.next().equalsIgnoreCase(username) && scPass.next().equals(password))
{
loggedIn = true;
scUser.close();
scPass.close();
new Selection(username).setVisible(true);
this.dispose();
break;
}
}
scUser.close();
scPass.close();
if(loggedIn == false)
{
JOptionPane.showMessageDialog(null,"Incorrect Username or Password!");
}
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
启动时映射所有用户,然后只检查地图。使用split来分隔用户名和密码。在调用onLoginCorrect()之前,请检查输入是否为空
private static final Logger LOG = LogManager.getLogger();
private final Map<String, String> users = new ConcurrentHashMap<>();
public Login() {
super();
final InputStream resource = this.getClass().getResourceAsStream("/users.txt");
final Scanner scanner = new Scanner(resource);
while (scanner.hasNext()) {
final String input = scanner.nextLine();
final String[] userPw = StringUtils.split(input, ':');
this.users.put(userPw[0], userPw[1]);
}
scanner.close();
IOUtils.closeQuietly(resource);
}
public boolean isLoginCorrect(final String username,final String password) {
final boolean result;
if (StringUtils.equals(this.users.get(username), password)) {
result = true;
} else {
Login.LOG.warn("No such user");
result = false;
}
return result;
}
我正在编写一个程序,读取文本文件,并显示第一个学生的姓名、年级和全班平均成绩。对于上面给出的文件,结果如下:类中的第一个是Ahmad Hamwi has 16.00,类的平均值是12.25这是我试图读取的W文本文件 这就是我一直犯的错误 我已经试了几个小时了。我知道错误在第37行。这可能与类型有关。我尝试了int和浮动,但一样。
我对JavaFX和JavaFX Scene Builder是新手,几个星期以来一直在研究并试图弄清楚如何简单地从文本文件中读取并在文本中显示其内容。我的controller类中有一个很好的文件读取功能,但我不知道如何将文本文件显示到fxml文档中的文本区域。我已经学会了如何单击按钮并使文件显示在文本区域中,但我希望GUI加载后立即在文本区域中显示内容。如果任何人有一个想法如何去做这件事,你的帮助将
问题内容: 我正在尝试读取资源(asdf.txt),但是如果文件大于5000字节,例如,在content变量的末尾插入了4700个空字符。有什么办法可以删除它们?(或设置缓冲区的正确大小?) 这是代码: 问题答案: 最简单的方法是做正确的事情:使用阅读器读取文本数据: 并非 一定 要定义要读取的文本文件的编码。在上面的示例中,它将是THE_ENCODING。 请注意,您的代码和本示例代码在Java
我有以下元素。计数在跨度元素中,文本在 DIV 标记中。当我将鼠标悬停在计数上时,它只读取数字,当我将鼠标悬停在 DIV 标记的文本上时,它只读取文本。但我希望屏幕阅读器将完整文本读取为“1 文档”。为此,我保留了带有计数的“aria标签”属性,并为具有类“列表项”的父DIV保留了文本。但它仍然只阅读文本“文档”,而不是整个计数。
我的项目中有这样一段代码: 没有错误,应用程序运行正常,但是变量中从来没有任何文本,我确信txt文件中有文本! 我已经尝试过不同的方法来读取文本文件(使用BufferedReader、Scanner、FileInputStream和FileReader),但都不起作用。 另外,我几乎可以肯定问题不在变量中,因为我尝试通过代码(使用运行时)打开文件,它正常打开了正确的文件。 好的,我尝试添加,但是仍