Java将字符串和字符串[]传递到另一个类中
我确信这个问题已经得到了回答,当我搜索它时,我得到了各种各样的结果,但我就是不能理解这个概念。这是一个家庭作业,我更愿意理解这就是为什么我张贴。任务是从文件中读取用户凭据,对密码进行哈希运算,然后如果它们与另一个与其角色关联的文件的显示内容相匹配,则显示它们 我在一个类中编写了这篇文章,然后发现这个赋值至少需要两个类。因此,在一节课上阅读文件,在另一节课上做其他事情对我来说是有意义的。一节课的效果非常好,但这是我的第一次编程冒险,我只上了6节课。我不理解我应该理解的基本知识,因此在您的回复中,如果您能告诉我为什么代码需要修改,我将不胜感激。我的代码如下Java将字符串和字符串[]传递到另一个类中,java,string,Java,String,我确信这个问题已经得到了回答,当我搜索它时,我得到了各种各样的结果,但我就是不能理解这个概念。这是一个家庭作业,我更愿意理解这就是为什么我张贴。任务是从文件中读取用户凭据,对密码进行哈希运算,然后如果它们与另一个与其角色关联的文件的显示内容相匹配,则显示它们 我在一个类中编写了这篇文章,然后发现这个赋值至少需要两个类。因此,在一节课上阅读文件,在另一节课上做其他事情对我来说是有意义的。一节课的效果非常好,但这是我的第一次编程冒险,我只上了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++;
}
}
}
}
我知道它不整洁,我相信有更好的方式让我写这样的东西,我只是做了我想做的和我知道的。我想如果有人能告诉我如何将这些字符串(管理员、兽医和动物园管理员)与字符串[]、userA userB等一起传递到我的main中,它将再次起作用,并足以满足具有我技能水平的人的需要
干杯
Andy由于您处于初级编码阶段,我将尝试帮助您了解一点逻辑,这对于制作一个好的应用程序至关重要 为了让自己更容易,你应该认为每项任务都需要一门课。在您的情况下,您应该有一个类用于获取文件,另一个用于检查密码。请记住,始终有一个主类启动应用程序并包含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
}
由于您处于入门级编码阶段,我将尝试帮助您稍微理解一下逻辑,这对于制作一个好的应用程序至关重要 为了让自己更容易,你应该认为每项任务都需要一门课。在您的情况下,您应该有一个类用于获取文件,另一个用于检查密码。请记住,始终有一个主类启动应用程序并包含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
}
你写了主要的方法;因此,您了解必须如何对
字符串[]
进行排序。我建议您从List
创建一个String[]
,其中包含需要传递的所有字符串;因此,您了解必须如何对字符串[]
进行排序。我建议您从List
创建一个String[]
,其中包含所有需要传递的字符串。我想我理解您在这里所说的内容,如果我是正确的,则需要进行大量重写。我不介意这样做,如果我能更好地掌握我正在做的事情,我花了足够长的时间让上面的代码正常工作,我想我没有时间重写整件事。我要说的是,尽管这一直是个挑战,但这是我所学过的最有趣的课程之一:)。一开始有点棘手,但当你掌握了窍门,你就会成为一个天才。您的代码没有错,但可以更好地结构化,从而帮助您满足需求。由于你只需要有两个类,我会保留你已经有的,并尝试添加另一个类,正如我所展示的。我想我理解你在这里说的,如果我是正确的,那将需要相当多的重写。我不介意这样做,如果我能更好地掌握我正在做的事情,我花了足够长的时间让上面的代码正常工作,我想我没有时间重写整件事。我要说的是,尽管这一直是个挑战,但这是我所学过的最有趣的课程之一:)。一开始有点棘手,但当你掌握了窍门,你就会成为一个天才。您的代码没有错,但可以更好地结构化,从而帮助您满足需求。由于您只需要有2个类,我会保留您已有的,并尝试添加另一个类,如我所示。