字符串Java帮助检查登录类使用。CSV文件
我目前正在尝试从.CSV
文件中检索信息,该文件包含以下形式的用户腰部凭据信息:
UserName,Password,PropertyName,PropertyValue
UserName,Password,PropertyName,PropertyValue
因此,我找到了一种使用split()函数分离用户名信息的方法。我现在很难在我的CLI类中使用这些信息,我会在其中搜索输入程序命令行的用户名是否与csv文件中的用户名匹配,以及是否与给定的密码匹配。任何帮助都将不胜感激
import java.util.*;
import java.io.*;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class readCSV {
private String[] userData;
public void checkLogin() throws IOException
{
String fileName = "C:/Users/Sean/Documents/Programming assigment/Users.csv";
File file = new File(fileName);
try
{
Scanner inputStream = new Scanner(file);
while(inputStream.hasNext()){
{
String data = inputStream.next();
userData = data.split(",");
}
}
} catch(FileNotFoundException er){
System.out.print(er);
System.exit(0);
}
}
public String getLogin()
{
return userData[0];
}
}
CLI
import java.util.*;
import java.io.*;
public class CLI
{
Scanner input = new Scanner(System.in);
private readCSV l1 = new readCSV();
public void login() throws IOException
{
System.out.println("Enter Username:");
String username = input.nextLine();
System.out.println("Enter Password:");
String password = input.nextLine();
try{
l1.checkLogin();
}
catch(Exception er){ System.out.print(er); }
卡在这行代码上检查用户名和密码
# 1 楼答案
你的程序设计没有经过深思熟虑。理想情况下,
checkLogin
方法应该接受用户名和密码的两个String
参数,然后返回一个boolean
来确定登录凭据是否正确:显然,在
login
方法中,您会将输入的用户名和密码传递到checkLogin
方法中: