简体   繁体   English

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

[英]Java Passing strings and string[] into another class

I am sure this question is answered, I get all kinds of results when I search on it, but I just cant grasp this concept. 我确信这个问题已得到解答,当我搜索它时会得到各种结果,但我无法理解这个概念。 This is a homework assignment and I prefer to understand which is why I am posting. 这是一个家庭作业,我更喜欢理解这是我发帖的原因。 The assignment is to read user credentials from a file, hash the password, and then if they match display contents of another file that associates with their role. 分配是从文件中读取用户凭据,散列密码,然后匹配与其角色关联的另一个文件的显示内容。

I wrote this in a single class and then discovered that the assignment calls for at least two classes. 我在一个类中编写了这个,然后发现赋值至少需要两个类。 So it made sense to me to read the files in 1 class and do everything else in another. 因此,对于我来说,读取1个类中的文件并在另一个类中执行其他操作是有意义的。 It worked very well as one class, but this is my first programming adventure and im only 6 classes in. I do not understand the basics as I should, so in your response if you can teach me why the code needs modified as it does I would be grateful. 它作为一个班级非常好用,但这是我的第一个编程冒险,我只有6个班级。我不理解我应该的基础知识,所以在你的回答中,如果你可以教我为什么代码需要修改,因为它是我将感激不尽。 My code is as follows; 我的代码如下;

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++;
        }







    }




}

You can see I started to create some objects but I just cannot figure out how, or if thats even a good way, to get info from my other class. 你可以看到我开始创建一些对象,但我无法弄清楚如何,或者如果这是一个好方法,从我的其他类获取信息。 And the class I created to read the files is; 我创建的用于读取文件的类是;

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

                }

     }

} }

I know its not neat and tidy and I am sure there are better ways for me write something like this, I just did what I though of and what I know. 我知道它不干净整洁,我相信有更好的方法让我写这样的东西,我只是按照我所做的和我所知道的去做。 I think if somebody could just show me how to pass those strings(admin, veterinarian, and zookeeper) along with the String[], userA userB etc. into my main it would work again and be sufficient for somebody with my skill level. 我想如果有人能告诉我如何将这些字符串(管理员,兽医和动物园管理员)与String [],userA userB等一起传递到我的主要部分,它将再次起作用,对于具有我技能水平的人来说已经足够了。

Cheers Andy 干杯安迪

As you are at entry level coding i would try to help you understand the logic a little which is essential to making a good application. 当你在入门级编码时,我会尽力帮助你理解一点对于制作一个好的应用程序至关重要的逻辑。

To make it easier for yourself you should think that each task needs a class. 为了让自己更容易,你应该认为每个任务都需要一个类。 In your case you should have one class for obtaining the files and another for checking password. 在您的情况下,您应该有一个类用于获取文件,另一个类用于检查密码。 keep in mind that there is always one dominant class that launches the application and contains the main method. 请记住,始终有一个主导类启动应用程序并包含main方法。

In these individual classes you should create methods to break down the processes making the code more clear. 在这些单独的类中,您应该创建方法来分解使代码更清晰的过程。 In the password checking class you would want a method for each of these jobs (reading the files, encrypting password, decrypting password, checking the credentials against eachother). 在密码检查类中,您需要一种方法来处理这些作业(读取文件,加密密码,解密密码,检查凭证等)。

Then return the value back to the first class. 然后将值返回到第一个类。 So it would look something like this. 所以它看起来像这样。

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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM