[英]Java help check loginIn class using .CSV files
我目前正在尝试从.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];
}
}
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); }
卡在这行代码上以检查用户名和密码
您的程序设计不是经过深思熟虑的。 理想情况下,您的checkLogin
方法应该接受两个String
参数作为用户名和密码,然后返回一个boolean
以确定登录凭据是否正确:
public boolean checkLogin(String username, String password) {
// Read CSV file, compare entries against provided username and password.
// Return true if a match is found. Otherwise, return false.
}
然后显然在您的login
方法中,您将输入的用户名和密码传递到checkLogin
方法中:
boolean loggedIn = false;
try {
loggedIn = checkLogin(username, password);
} catch(Exception ex) {
ex.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.