gpt4 book ai didi

Java 将字符串和 string[] 传递给另一个类

转载 作者:太空宇宙 更新时间:2023-11-04 12:10:14 24 4
gpt4 key购买 nike

我确信这个问题已经得到解答,当我搜索它时,我得到了各种各样的结果,但我就是无法理解这个概念。这是一项家庭作业,我更愿意理解这就是我发帖的原因。任务是从文件中读取用户凭据,对密码进行哈希处理,然后如果它们匹配,则显示与其角色关联的另一个文件的内容。

我在一个类中编写了这个,然后发现该作业至少需要两个类。因此,对我来说,阅读一个类中的文件并在另一个类中执行其他所有操作是有意义的。它作为一门课效果很好,但这是我的第一次编程冒险,我只上了 6 门课。我不了解我应该理解的基础知识,所以在你的回复中,如果你能告诉我为什么代码需要修改,我将不胜感激。我的代码如下;

package it145_final;
import java.util.Scanner;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class IT145_Final {

public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
Scanner scnr = new Scanner(System.in);
Scanner fileIn = null;
int failedAttempts = 0;
int i = 0;
String q = "q";
// objects that I think I need???? Maybe??? but dont know how to get them from the FinalFiles class
FinalFiles fileAdmin = new FinalFiles();
FinalFiles fileVet = new FinalFiles();
FinalFiles fileZoo = new FinalFiles();
FinalFiles userA = new FinalFiles();
fileAdmin.file();
userA.file();

while (failedAttempts < 3)
{
System.out.println("Enter user name, or q to exit"); //get username
String userName = scnr.next();
if (userName.equalsIgnoreCase(q)) //option to terminiate
{
System.out.println("Logging Out");
break;
}
System.out.println("Enter password"); // get password
scnr.nextLine();
String userPassword = scnr.nextLine();
//The following takes the entered password and hashes it
String hashedPass = userPassword;
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(hashedPass.getBytes());
byte[] digest = md.digest();
StringBuffer sb = new StringBuffer();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}


if (userName.equals(userA[i]) && sb.toString().equals(userA[i + 1]))
{
if (userA[i + 3].equals("admin"))
{
System.out.println(admin);
break;
}
else if (userA[i + 3].equals("veterinarian"))
{
System.out.println(veterinarian);
break;

}
else if (userA[i + 3].equals("zookeeper"))
{
System.out.println(zookeeper);
break;

}
else
{
System.out.println("Failed attempt");
failedAttempts ++;
}

}
if (userName.equals(userB[i]) && sb.toString().equals(userB[i + 1]))
{
if (userB[i + 3].equals("admin"))
{
System.out.println(admin);
break;

}
else if (userB[i + 3].equals("veterinarian"))
{
System.out.println(veterinarian);
break;
}
else if (userB[i + 3].equals("zookeeper"))
{
System.out.println(zookeeper);
break;
}
else
{
System.out.println("Failed attempt");
failedAttempts ++;
}

}
if (userName.equals(userC[i]) && sb.toString().equals(userC[i + 1]))
{
if (userC[i + 3].equals("admin"))
{
System.out.println(admin);
break;
}
else if (userC[i + 3].equals("veterinarian"))
{
System.out.println(veterinarian);
break;
}
else if (userC[i + 3].equals("zookeeper"))
{
System.out.println(zookeeper);
break;
}
else
{
System.out.println("Failed attempt");
failedAttempts ++;
}

}
if (userName.equals(userD[i]) && sb.toString().equals(userD[i + 1]))
{
if (userD[i + 3].equals("admin"))
{
System.out.println(admin);
break;
}
else if (userD[i + 3].equals("veterinarian"))
{
System.out.println(veterinarian);
break;
}
else if (userD[i + 3].equals("zookeeper"))
{
System.out.println(zookeeper);
break;
}
else
{
System.out.println("Failed attempt");
failedAttempts ++;
}

}
if (userName.equals(userE[i]) && sb.toString().equals(userE[i + 1]))
{
if (userE[i + 3].equals("admin"))
{
System.out.println(admin);
break;
}
else if (userE[i + 3].equals("veterinarian"))
{
System.out.println(veterinarian);
break;
}
else if (userE[i + 3].equals("zookeeper"))
{
System.out.println(zookeeper);
break;
}
else
{
System.out.println("Failed attempt");
failedAttempts ++;
}

}
if (userName.equals(userF[i]) && sb.toString().equals(userF[i + 1]))
{
if (userF[i + 3].equals("admin"))
{
System.out.println(admin);
break;
}
else if (userF[i + 3].equals("veterinarian"))
{
System.out.println(veterinarian);
break;
}
else if (userF[i + 3].equals("zookeeper"))
{
System.out.println(zookeeper);
break;
}
else
{
System.out.println("Failed attempt");
failedAttempts ++;
}

}


System.out.println("Login Failed");
failedAttempts++;
}







}




}

您可以看到我开始创建一些对象,但我只是不知道如何从其他类(class)获取信息,或者这是否是一个好方法。我创建的用于读取文件的类是;

package it145_final;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;


public class FinalFiles {
public static void file() throws IOException{


String admin = "";
String veterinarian = "";
String zookeeper = "";
String[] userA = new String[4];
String[] userB = new String[4];
String[] userC = new String[4];
String[] userD = new String[4];
String[] userE = new String[4];
String[] userF = new String[4];

File file0 = new File("C:usercredentials.txt"); // Opens files
Scanner contents0 = new Scanner(file0);
File file1 = new File("C:admin.txt");
Scanner contents1 = new Scanner(file1);
File file2 = new File("C:veterinarian.txt");
Scanner contents2 = new Scanner(file2);
File file3 = new File("C:zookeeper.txt");
Scanner contents3 = new Scanner(file3);

// Following reads the files and assignes to variables as needed
while (contents1.hasNext())
{
admin += contents1.nextLine();
}
// System.out.println(admin); used to verify that admin was correct
while (contents2.hasNext())
{
veterinarian += contents2.nextLine();
}
while (contents3.hasNext())
{
zookeeper += contents3.nextLine();
}
while(contents0.hasNext())
{
String user1 = contents0.nextLine();//grabs the line from the file for each individual user
String user2 = contents0.nextLine();
String user3 = contents0.nextLine();
String user4 = contents0.nextLine();
String user5 = contents0.nextLine();
String user6 = contents0.nextLine();
userA = user1.split("\t");//takes information on user and breaks it into an array
userB = user2.split("\t");
userC = user3.split("\t");
userD = user4.split("\t");
userE = user5.split("\t");
userF = user6.split("\t");

System.out.println(userB[0]); //using for testing to make sure I am getting the correct info
System.out.println(userB[1]);

}

}

}

我知道它不整洁,我确信我有更好的方法来写这样的东西,我只是做了我想到的和我知道的。我认为如果有人可以向我展示如何将这些字符串(管理员、 vert 和动物园管理员)以及 String[]、userA userB 等传递到我的 main 中,那么它会再次起作用,并且对于具有我的技能水平的人来说已经足够了。

干杯安迪

最佳答案

由于您处于入门级编码阶段,我会尽力帮助您理解一些逻辑,这对于制作良好的应用程序至关重要。

为了让自己更轻松,您应该认为每个任务都需要一个类。在你的情况下,你应该有一个类用于获取文件,另一个类用于检查密码。请记住,始终有一个主导类启动应用程序并包含 main 方法。

在这些单独的类中,您应该创建方法来分解流程,使代码更加清晰。在密码检查类中,您需要为每个作业提供一个方法(读取文件、加密密码、解密密码、相互检查凭据)。

然后将值返回给第一个类。所以它看起来像这样。

class Main { //first class
public static void main(String[] args){
File = new File("file1"); //obtain file 1
File = new File("file2"); //obtain file 2
PasswordCheck checker = new PasswordCheck(); // call instance of second class

boolean credentialOk= passwordCheck.process(file1,file2)//calls method in second class and returns if the credentials match
}
}

class PasswordCheck { //second class
public passwordCheck(){

}//inistialise class

public boolean process(File file1, File file2){

}// method to process the files and returns if match succesfully or not
}

关于Java 将字符串和 string[] 传递给另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39932763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com