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

时间:2016-10-08 13:11:44

标签: java string

我确信这个问题已得到解答,当我搜索它时会得到各种结果,但我无法理解这个概念。这是一个家庭作业,我更喜欢理解这是我发帖的原因。分配是从文件中读取用户凭据,散列密码,然后匹配与其角色关联的另一个文件的显示内容。

我在一个类中编写了这个,然后发现该赋值至少需要两个类。因此,对于我来说,读取1个类中的文件并在另一个类中执行其他操作是有意义的。它作为一个班级非常好用,但这是我的第一个编程冒险,我只有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++;
        }







    }




}

你可以看到我开始创建一些对象,但我无法弄清楚如何,或者如果这是一个好方法,从我的其他类获取信息。 我创建的用于读取文件的类是;

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]);

                }

     }

}

我知道它不干净整洁,我相信有更好的方式让我写这样的东西,我只是做了我喜欢的和我所知道的。我想如果有人能告诉我如何将这些字符串(管理员,兽医和动物园管理员)以及String [],userA userB等传递到我的主要部分,那么它将再次起作用,对于具有我技能水平的人来说已经足够了。 / p>

干杯 安迪

1 个答案:

答案 0 :(得分:0)

当你在入门级编码时,我会尽力帮助你理解一点对于制作一个好的应用程序至关重要的逻辑。

为了让自己更容易,你应该认为每个任务都需要一个班级。在您的情况下,您应该有一个类用于获取文件,另一个类用于检查密码。请记住,始终有一个主导类启动应用程序并包含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 
}