数组中的数组奇怪吗? Java的

时间:2017-03-31 19:13:30

标签: java arrays

我有一个变量B,用于存储在数组userarr中添加了多少个用户对象。我正在使用一行代码添加用户输入B数组中userarr元素用户对象的注释。这是:

userarr[login].notes[b] = tempnote;

tempnote是一个字符串变量,暂时保存用户键入的注释,login存储您登录的用户编号。因此,它将字符串变量tempnote分配给login数组中的user值用户对象,以及该用户的notes数组中的元素b元素。

但由于某种原因,这行代码存在问题。 我知道这是代码行,因为它是在您确认要添加该注释后立即发生的,并且旁边有一个永远不会出现的println

以下是整个音符的方法:

 public static void loggedin() {
     System.out.println("welcome, " + userarr[login].username + "!");
     do{
         System.out.println("type 'notes' to list current notes, type 'new' to add notes, type 'delete' to delete a note, or type 'exit' to exit the program.");
         switch(scan.nextLine()){
             case "exit":
                 System.out.println("exiting");
                 login = -1;
                 break;
              case "delete":

                 break;
              case "new":
                 System.out.println("\n\nType the note.");
                 String tempnote = scan.nextLine();
                 System.out.println("note is is now " + tempnote + ". is this what you want? type 'yes' to proceed, or 'no' to enter note again.");
                 String ch5 = scan.nextLine();
                 if (ch5.equals("no")) {
                     break;
                 } else {
                    userarr[login].notes[b] = tempnote;
                    System.out.println("note created!");
                    b += 1;
                 }
                 break;
             case "notes":
                 for (int i=0;i<b;i++) {
                     System.out.println("Note " + i + ":");
                     System.out.println(userarr[login].notes[i] + "\n");
                 }
                 break;
             default: 
                 System.out.println("restarting.");

         };
     }while(login != -1);
}

这是用户对象:

 static class user extends TextGame {
     String username;
     int password;
     String[] notes;

     public user(String username, int password) {
        this.username = username;
        this.password = password;
     }

 }
 static user[] userarr = new user[10];
 static int a = 0;
 static int b = 0;
 static int login = -1;

运行之前没有错误。当我到达有问题的部分时说:

Exception in thread "main" java.lang.NullPointerException
at text.game.TextGame.loggedin(TextGame.java:80)
at text.game.TextGame.login(TextGame.java:53)
at text.game.TextGame.main(TextGame.java:135)
有人知道这个问题吗?

编辑:似乎有必要向全班展示,因为显然人们需要了解更多信息。所以这里有你需要的所有信息:

package text.game;
import java.util.Scanner;

public class TextGame {

    static Scanner scan = new Scanner(System.in);
    static class user extends TextGame {
        String username;
        int password;
        String[] notes;

        public user(String username, int password) {
            this.username = username;
            this.password = password;
        }

     }
     static user[] userarr = new user[10];
     static int a = 0;
     static int b = 0;
     static int login = -1;


     public static void newuser() {
         System.out.println("\n\nType the username for this user.");
         String usernameA = scan.nextLine();
         System.out.println("Username is now " + usernameA + ". is this what you want? type 'yes' to proceed, or 'no' to enter username again.");
         if (scan.nextLine().equals("no")) {
             newuser();
         } else {
             System.out.println("\n\n type the password for this user. (numbers only.)");
             int passwordA = scan.nextInt();
             System.out.println("user is " + usernameA + " and password is " + passwordA + ". creating user.");
             userarr[a] = new user(usernameA, passwordA);
             System.out.println("user created!");
             a += 1;
         }
     }

     public static void login() {
         System.out.println("which account do you want to log into? type the name of a user, or type 'list' to view the users signed in.");
         String ch2 = scan.nextLine();
         if (ch2.equals("list")){
             for (int i=0;i<a;i++) {
                 System.out.println(userarr[i].username);
             }
         } else {
             for (int i=0;i<a;i++) {if ((userarr[i].username).equals(ch2)){
                  System.out.println("type the password for this account (USE NUMBERS ONLY).");
                  int ch4 = scan.nextInt();
                  if (ch4==userarr[i].password) {
                      System.out.println("logged in!"); login = i; loggedin();
                  }else{
                      System.out.print("incorrect password!");
                  }
             }
         }
     }
 }

 public static void loggedin() {
     System.out.println("welcome, " + userarr[login].username + "!");
     do{
         System.out.println("type 'notes' to list current notes, type 'new' to add notes, type 'delete' to delete a note, or type 'exit' to exit the program.");
         switch(scan.nextLine()){
             case "exit":
                 System.out.println("exiting");
                 login = -1;
                 break;
             case "delete":

                 break;
             case "new":
                 System.out.println("\n\nType the note.");
                 String tempnote = scan.nextLine();
                 System.out.println("note is is now " + tempnote + ". is this what you want? type 'yes' to proceed, or 'no' to enter note again.");
                String ch5 = scan.nextLine();
                if (ch5.equals("no")) {
                    break;
                } else {
                    userarr[login].notes[b] = tempnote;
                    System.out.println("note created!");
                    b += 1;
                }
                break;
            case "notes":
                for (int i=0;i<b;i++) {
                    System.out.println("Note " + i + ":");
                    System.out.println(userarr[login].notes[i] + "\n");
                }
                break;
            default: 
                System.out.println("restarting.");

        };
    }while(login != -1);
}

 public static void main(String[] args) {
     do {
         System.out.println("Welcome to LINCOLN COMP console OS. Type 'new' to create a new user, type 'log' to log in to an existing user, or type 'exit' to leave.\nif you are asked a yes or no question, if you type something other than yes or no, it will default to yes.");
         String ch1 = scan.nextLine();
         switch (ch1) {
             case "new":
                 if (a==2) {
                     System.out.println("maximum users have been created. type a username to delete that user, type list to list users, or type back to return.");
                     String ch3 = scan.nextLine();
                     if (ch3.equals("list")) {
                         for (int i=0;i<a;i++) { 
                              if (userarr[i].username==null) {
                                  System.out.println("undefined");
                              }else{
                                  System.out.println("\n" + userarr[i].username);
                              };
                          }
                     } else if (ch3.equals("back")) {
                         break;
                     } else {
                          for (int i=0;i<a;i++) {
                              if ((userarr[i].username).equals(ch3)){
                                  System.out.println("type the password for this account (USE NUMBERS ONLY).");
                                  int ch4 = scan.nextInt();
                                  if (ch4==userarr[i].password) {
                                      a --;
                                      userarr[i] = null;
                                      System.out.println("user deleted!");
                                      break;
                                  }else{
                                      System.out.println("incorrect password.");
                                      break;
                                  }

                              }else if (i==a-1) { 
                                  System.out.println("user not found.");
                                  break;
                              }

                          }

                     } 
                 }else {
                     System.out.println("Initializing user creation method:");
                     newuser();
                 }
                 break;
             case "log":
                 login();
                 break;
             case "exit":
                 System.out.println("Goodbye!");
                 System.exit(0);
                 break;
             case "debug":
                 for (int i=0;i<userarr.length;i++) {
                     System.out.println(userarr[i]);
                 }
                 break;
             default:
                 System.out.println("restarting.");
         }

     } while (true);
 } 

}//Note from other user - Extra bracket?

2 个答案:

答案 0 :(得分:0)

更改

String[] notes;

类似于:

String[] notes = new String[10];

答案 1 :(得分:0)

我注意到,当您使用扫描仪的下一个功能时,您似乎没有检查是否有下一行/整行。

从您的代码中获取这两行代码......

String ch2 = scan.nextLine();

int ch4 = scan.nextInt();

您可以使用hasNext函数检查是否确实存在下一个int或下一行......

String ch2; //Declared outside of the if-statement for scope reasons
if(scan.hasNextLine()){
    //the hasNext functions return true if there is a next line (or int for hasNextInt).
    ch2 = scan.nextLine();
}else{
    //if something needs to be done in the event that there is no nextLine, do it here
}

int ch4;
if(scan.hasNextInt()){
    ch4 = scan.hasNextInt();
}else{
    //if something needs to be done in the event that there is no nextInt, do it here
}

这可能无法解决您的问题;但是,这至少可以防止以后出现许多潜在问题。你应该始终确保在获得之前有更多的内容,并且使用hasNext函数可以非常容易。

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

了解详情