Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为我的身份验证表程序生成新表?_Java_Arrays_Authentication - Fatal编程技术网

Java 如何为我的身份验证表程序生成新表?

Java 如何为我的身份验证表程序生成新表?,java,arrays,authentication,Java,Arrays,Authentication,我正在用netbeans构建一个关于身份验证表的小java程序。我已经构建了表,并开始构建程序使用的任务。我的编程遇到了几个问题 主程序代码: package ass2resub; //Importing the java facilities I need for my program to run. import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * * @author Jacob

我正在用netbeans构建一个关于身份验证表的小java程序。我已经构建了表,并开始构建程序使用的任务。我的编程遇到了几个问题

主程序代码:

package ass2resub;

//Importing the java facilities I need for my program to run.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
*
* @author Jacob
*/
public class Ass2Resub {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
     //I am using a scanner to get input from the user.
    Scanner scan = new Scanner(System.in);

//This is the way I am going to start the program. Its similar to an arraylist or linkedlist.
AuthenticationProviderBigram authProvider = new AuthenticationProviderBigram();

//This is all the data for my authentication table. The first column represents the co-ordinates that are on the table./n
//The second column represents the code within the table to be authenticated.
//This is pre-entered details so that you can have a feel for how the program will work.
authProvider.addAuthentication("A1", "Z1");
authProvider.addAuthentication("A2", "G4");
authProvider.addAuthentication("A3", "T8");
authProvider.addAuthentication("A4", "23");
authProvider.addAuthentication("A5", "N1");
authProvider.addAuthentication("A6", "Q1");
authProvider.addAuthentication("A7", "2B");
authProvider.addAuthentication("A8", "L3");
authProvider.addAuthentication("A9", "U7");

authProvider.addAuthentication("B1", "N4");
authProvider.addAuthentication("B2", "X1");
authProvider.addAuthentication("B3", "J8");
authProvider.addAuthentication("B4", "C9");
authProvider.addAuthentication("B5", "H9");
authProvider.addAuthentication("B6", "2U");
authProvider.addAuthentication("B7", "8R");
authProvider.addAuthentication("B8", "E7");
authProvider.addAuthentication("B9", "5K");

authProvider.addAuthentication("C1", "L3");
authProvider.addAuthentication("C2", "LS");
authProvider.addAuthentication("C3", "4S");
authProvider.addAuthentication("C4", "BH");
authProvider.addAuthentication("C5", "8D");
authProvider.addAuthentication("C6", "K9");
authProvider.addAuthentication("C7", "C3");
authProvider.addAuthentication("C8", "M6");
authProvider.addAuthentication("C9", "SK");

authProvider.addAuthentication("D1", "C4");
authProvider.addAuthentication("D2", "6X");
authProvider.addAuthentication("D3", "R4");
authProvider.addAuthentication("D4", "L9");
authProvider.addAuthentication("D5", "E5");
authProvider.addAuthentication("D6", "O7");
authProvider.addAuthentication("D7", "J5");
authProvider.addAuthentication("D8", "71");
authProvider.addAuthentication("D9", "6E");

authProvider.addAuthentication("E1", "9T");
authProvider.addAuthentication("E2", "4L");
authProvider.addAuthentication("E3", "B3");
authProvider.addAuthentication("E4", "AA");
authProvider.addAuthentication("E5", "9F");
authProvider.addAuthentication("E6", "8M");
authProvider.addAuthentication("E7", "8Y");
authProvider.addAuthentication("E8", "B1");
authProvider.addAuthentication("E9", "S8");

authProvider.addAuthentication("F1", "M9");
authProvider.addAuthentication("F2", "8U");
authProvider.addAuthentication("F3", "4J");
authProvider.addAuthentication("F4", "2S");
authProvider.addAuthentication("F5", "F3");
authProvider.addAuthentication("F6", "N8");
authProvider.addAuthentication("F7", "P7");
authProvider.addAuthentication("F8", "ZA");
authProvider.addAuthentication("F9", "P1");

authProvider.addAuthentication("G1", "O2");
authProvider.addAuthentication("G2", "4Q");
authProvider.addAuthentication("G3", "Y2");
authProvider.addAuthentication("G4", "K4");
authProvider.addAuthentication("G5", "B4");
authProvider.addAuthentication("G6", "ER");
authProvider.addAuthentication("G7", "T3");
authProvider.addAuthentication("G8", "H3");
authProvider.addAuthentication("G9", "D7");

//Declaring the names of the tasks the program is capable of.
//Once a selection is made it will load up that certain case and tell you what/n
//it needs to do its job.
  System.out.println("Authentication table program\n"
                + "1.Search for a certain co-ordinate to authenticate\n"
                + "2.Show the table\n"
                + "3.Generate a new table\n"
                + "4.Exit\n");

  //This is the while loop fnction that we se so that we can have the ability to go through the menu above.
        while(true) {

  //Declaring a integer variable for my switch so that I can gain input from the user so that it chooses/n
  //the one part of my program that they chose instead of all the rest of them all at once.
        int Choice = scan.nextInt();

switch (Choice) { //Start of my switch and cases.

    //The first function of the program is to search for the grid reference to see the/n
    //authentication code and to authenticate it.
   case 1:

          //This is the question that is displayed in the output box.
          System.out.print("The co-ordinates that your after: ");
          //This scans the next line to get the response from the user.
          String userInput = scan.next();
          //This then prints out the text and the code that is linked to the grid reference.
          System.out.print("I authenticate " + authProvider.authenticate(userInput));
   break;

   case 2:
       //This displays the table.
       System.out.print("This is the table: "  );
       break;

   case 3:

       break;

   case 4:
         //This is the case so that yo can exit ot of the program when you\n
                //have finished with it.

                System.out.println("Are you sure you want to exit the program(Yes(0) or No): ");
                scan.next();

                //Need an if function here so that I can act upon the users input.
                System.exit(0);

       break;
}
        }
}
}
我的java类的代码:

package ass2resub;
//Importing the java facilities I need for my program to run.
import java.util.Map;
import java.util.HashMap;
/**
*
* @author Jacob Fellows
*/
public class AuthenticationProviderBigram 
{//Start of class.
//I am using a hash map with a map as data structures which can store the revelant information.
//I am decaring my Map and calling it authTable for short.
private Map<String, String> authTable;

//This is the way to make authTable a new hashmap.
public AuthenticationProviderBigram()
{
    this.authTable = new HashMap<>();
}

//This will get the input from the user via the scanner.
public String authenticate(String input)
{
    return this.authTable.get(input);
}

//This is the way to add new details to the table. You use the co-ordinates/n
//of the square and the code inside.
public void addAuthentication(String source, String expected)
{
    if(!this.authTable.containsKey(source))
        this.authTable.put(source, expected);
}

}//End of class.
package ass2resub;
//导入程序运行所需的java工具。
导入java.util.Map;
导入java.util.HashMap;
/**
*
*@作者雅各布·费罗斯
*/
公共类身份验证ProviderBigram
{//开始上课。
//我使用的是一个散列映射和一个映射作为数据结构,它可以存储相关的信息。
//我正在对我的地图进行衰减,简称为authTable。
私有地图授权表;
//这是使authTable成为新hashmap的方法。
公共身份验证ProviderBigram()
{
this.authTable=new HashMap();
}
//这将通过扫描仪从用户处获取输入。
公共字符串身份验证(字符串输入)
{
返回this.authTable.get(输入);
}
//这是向表中添加新详细信息的方法。您使用坐标/n
//正方形和里面的代码。
public void addAuthentication(字符串源,应为字符串)
{
如果(!this.authTable.containsKey(源))
this.authTable.put(源,应为);
}
}//下课了。
这就是我目前掌握的代码

问题:

  • 如何在java中以格式化的方式显示表数据,以便用户可以按顺序查看

  • 我如何生成一个新表,其中包含空白数据,或者可以从用户输入数据来构建简单表

  • 正如您在主程序中所看到的,我正在使用switch和case方法创建一个无限循环

    谢谢,
    雅各布。

    1.以格式化的方式显示数据

     public void showTable() {
        System.out.println("| Co-ordinate |  Code |");
        System.out.println("|---------------------|");
        for (String key : authTable.keySet()) {
            System.out.println("| " + key + "  |  " + authTable.get(key) + " |");
        }
        System.out.println("|---------------------|");
    }
    
    //In you mainclass
     case 2:
                    //This displays the table.
                    System.out.println("This is the table: ");
                    authProvider.showTable();
                    break;
    
    2 a.生成新表意味着清空旧表[在您的情况下映射]

      public void clearTable() {
        authTable.clear();
      }
    
     case 3:
                    authProvider.clearTable();
                    System.out.println("Table Emptied ");
                    break;
    
    2 b.要求用户输入数据。
    为开关添加新的外壳。对于ex
    5。输入新值

      case 5:
                    System.out.print("Enter The co-ordinates you want to put: ");
                    //This scans the next line to get the response from the user.
                    userInput = scan.next();
                    System.out.print("Enter the code value ");
                    String codeValue = scan.next();
                    authProvider.addAuthentication(userInput, codeValue);
                    break;
    
    一些建议:1)为了清晰起见,您应该使用命名常量(静态final int)或枚举(更好的是,使用基于命令和策略设计模式的多态性);2) 您应该在外部存储配置并加载它(我认为您打算根据您的问题这样做);3) 如果这是为了严肃的(生产、非玩具、非学校)目的,那么应该使用经过验证的身份验证机制(SAML、JAAS、OAuth、OpenID)