简体   繁体   English

如何在我的登录系统中实现String.split()?

[英]How can I implement String.split() in my login system?

I have a text file called UserDetails.txt that I am trying to read from. 我有一个要读取的名为UserDetails.txt的文本文件。

Each line of the text file is as follows: 文本文件的每一行如下:

John : Doe : Seattle : jd3 : 1234 约翰:母鹿:西雅图:jd3:1234

Jane : Doe : Olympia : jd4 : 5678 简:母鹿:奥林匹亚:jd4:5678

Jerry : Doe : Redmond : jd5 : 9101 杰里:母鹿:雷德蒙德:jd5:9101

And so on... 等等...

Each line has the first name, last name, username, and password of the registered user by which I am trying to search for only the last two variables (username and password). 每行都有注册用户的名字,姓氏,用户名和密码,我试图通过它们仅搜索最后两个变量(用户名和密码)。

public class LoginFrame extends javax.swing.JFrame 
{


private static Scanner keyboard = new 
Scanner(System.in);
String username;
String password;
String filePath = "UserDetails.txt";

public LoginFrame() {
initComponents();
}

private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt) {                                             
username = jTextFieldUsername.getText();
password = jTextFieldPassword.getText();
verifyLogin(username,password,filePath);
}                                            

public static void verifyLogin(String username, 
String password, String filepath)
{
boolean match = false;
String tempUserName = "";
String tempPassword = "";
   try
   {
   keyboard = new Scanner(new 
   File(filepath));
   keyboard.useDelimiter("[:\n]");

   while(keyboard.hasNext() && !match)
   {
       tempUserName = keyboard.next();
       tempPassword = keyboard.next();

       if(tempUserName.trim().equals(username.trim()) && 
tempPassword.trim().equals(password.trim()))
       {
           match = true;
       }
   }
   keyboard.close();
   System.out.print(match);
}
catch (Exception e)
{
   System.out.print("Error");
}
}

This above code snippet is my original code by which I tried to use a delimiter to find the two specific values but it only seems to work if the username and password are the only two variables in the text file (with first and last names removed). 上面的代码片段是我的原始代码,我试图通过它使用定界符来找到两个特定值,但只有在用户名和密码是文本文件中仅有的两个变量(删除了名字和姓氏)的情况下,它才似乎起作用。

I've been reading up on the String.split() method so that I can replace my original use of the delimiter. 我一直在阅读String.split()方法,以便可以替换定界符的原始用法。 However, I'm still struggling with how I can apply it to my text file. 但是,我仍在努力将其应用于文本文件。 Many of the examples online explain how one can convert an individual line into a String array (which in my example, would have the username at index 3 and password at index 4). 在线上的许多示例说明了如何将一行转换为String数组(在我的示例中,该用户名的索引为3,密码的索引为4)。 This is where I'm confused though. 这是我很困惑的地方。 How can I implement the String.split() method without having to manually input it for every specific line? 如何实现String.split()方法,而不必为每个特定行手动输入它? (since there are 50 users in the text file). (因为文本文件中有50个用户)。 Would it be possible to implement it with the Scanner.nextLine() method? 可以使用Scanner.nextLine()方法实现它吗?

Here: 这里:

while(keyboard.hasNext() && !match) { tempUserName = keyboard.next(); while(keyboard.hasNext()&&!match){tempUserName = keyboard.next(); tempPassword = keyboard.next(); tempPassword = keyboard.next();

You are reading each of the lines in pairs. 您正在成对读取每一行。 You should instead call keyboard.next four times in each iteration. 您应该在每次迭代中四次调用keyboard.next I am guessing that you intend to ignore the first name and last name, so you don't need to assign them to any variable: 我猜您打算忽略名字和姓氏,因此您无需将它们分配给任何变量:

while(keyboard.hasNext() && !match) { // These two lines read the first name and last name and do nothing with them keyboard.next(); while(keyboard.hasNext()&&!match){//这两行读取名字和姓氏,而对它们不做任何操作keyboard.next(); keyboard.next(); keyboard.next();

   // these will read the username and password
   tempUserName = keyboard.next();
   tempPassword = keyboard.next();

If you want to use split , you need to call nextLine and hasNextLine instead: 如果要使用split ,则需要调用nextLinehasNextLine

while (keyboard.hasNextLine() && !match) {
    String[] parts = keyboard.nextLine().split(" : ");
    tempUserName = parts[2];
    tempPassword = parts[3];
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 String.split() 如何根据不包括某个字符串的正则表达式拆分字符串 - Using String.split() How can I split a string based on a regular expression excluding a certain string 如何在不使用 String.split() 的情况下将字符串拆分为 Java 中的单词? - How can I split a string into words in Java without using String.split()? Java:尝试拆分成“”,但不接受string.split(“””)…该怎么做? - Java: Trying to split in “ , but string.split(”“”) is not accepted…how can this be done? 我如何使用string.split方法将空白条目添加到空白字段中? - How can i use string.split method to add empty entries into the empty fields ? String.split(“。”)没有拆分我的长字符串 - String.split(“.”) is not splitting my long String String.split() - 如何将连续分隔符视为一个? - String.split() — How do I treat consecutive delimiters as one? 如何为我的api使用系统实现登录 - How would I implement a login with my system for my api 如何提高String.split的性能? - How to improve performance of String.split? 如何使用String.split()拆分字符串而不使用尾随/前导空格或空值? - How do I split string using String.split() without having trailing/leading spaces or empty values? 在这种情况下如何使用String.split? - How to using String.split in this case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM