有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

字符串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) 个答案

  1. # 1 楼答案

    你的程序设计没有经过深思熟虑。理想情况下,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();
    }