简体   繁体   English

Java - 如何通过 main 方法在不同的类中存储变量?

[英]Java - How to store a variable via main method in a different class?

I'm supposed to store variables of an object based on user input inside a main method, to a different class.我应该将基于用户输入的对象变量存储在主方法中,到不同的类。 I'm getting static to non-static reference errors for "gerbil.id" and "gerbil.name";我得到了“gerbil.id”和“gerbil.name”的静态到非静态参考错误; I don't even know if it would save to the variables in the object in the gerbil class.我什至不知道它是否会保存到沙鼠类中对象的变量中。 Any ideas?有任何想法吗? Here's what I have so far:这是我到目前为止所拥有的:

EDIT: Now I get a nullpointerexceptor when I run with the fixes posted;编辑:现在,当我运行发布的修复程序时,我得到了一个 nullpointerexceptor; here is the updated code.这是更新的代码。 the exception points to the gerbilArray[i].id = keyboard.next() line specifically.异常指向 gerbilArray[i].id = keyboard.next() 行。 I'm not sure what could've caused it.我不确定是什么原因造成的。 I've listed id as a string and also input a string (one word) when run.我已经将 id 列为一个字符串,并在运行时输入一个字符串(一个单词)。

Inside of my information class:在我的信息类里面:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many gerbils in the lab?");
    int numberOfGerbils = keyboard.nextInt();
    Gerbil[] gerbilArray = new Gerbil[numberOfGerbils];

    for(int i = 0; i < numberOfGerbils; i++){

        System.out.print("Lab ID:");
        gerbilArray[i].id = keyboard.next();

}

Inside of the gerbil class:沙鼠类的内部:

import java.util.Scanner;
public class Gerbil {

public String id;


public Gerbil(String name) {
    this.id = "";
  }
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("How many gerbils in the lab?");
    int numberOfGerbils = keyboard.nextInt();
    Gerbil[] GerbilArray = new Gerbil[numberOfGerbils]; // initialization of array

    for(int i = 0; i < numberOfGerbils; i++){
        GerbilArray[i] = new Gerbil();
        System.out.print("Lab ID:");
        // String Gerbil.id = keyboard.next(); should be replaced by
        GerbilArray[i].id=keyboard.next(); // because you should call non static members using objects of a class. Here object is an array element which requires an array index.

        System.out.print("Gerbil Nickname:");
        //String name = keyboard.next(); replace by
        GerbilArray[i].name=keyboard.next();
        // as stated earlier. and you don't need to write String again because you have already declared it in the class declaration. If you try to do this way, you are creating a new String object only. This name has already been assigned to array, so it will result in an error.
    }
}

in the Gerbil class, contructor should be corrected as在 Gerbil 类中,构造函数应更正为

public Gerbil(String name, String id) {
    this.name = name;
    this.id = id;
}

because local members hides class members.因为本地成员隐藏了班级成员。 here name and id of constructor will hide name and id of class.这里构造函数的nameid将隐藏类的名称和 id。 the above statements does nothing.上面的语句没有任何作用。 it is an assignment made to itself only.它只是对自己进行的一项分配。 this keyword is used to point to invoking object. this关键字用于指向调用对象。

and since you are providing a parameterized constructor, but you are creating objects using default constructor, you need to explicitely create a default constructor as well.并且由于您提供了一个参数化构造函数,但是您正在使用默认构造函数创建对象,因此您还需要明确地创建一个默认构造函数。

Gerbil(){}

It should be它应该是

GerbilArray[i].id = keyboard.next();

instead of代替

Gerbil.id = keyboard.next();

It would help to avoid this kind of confusion if you followed the convention of starting variable names with lowercase letters.如果您遵循以小写字母开头的变量名约定,这将有助于避免这种混淆。 So then it would be:那么它将是:

gerbilArray[i].id = keyboard.next();

You are getting a NullPointerException as there is a problem in allocating the space for the array.您收到 NullPointerException,因为在为数组分配空间时出现问题。 For example: numberofGerbils=5 then gerbilArray[0],gerbilArray[1],gerbilArray[2],gerbilArray[3],gerbilArray[4] are created .例如: numberofGerbils=5 然后gerbilArray[0],gerbilArray[1],gerbilArray[2],gerbilArray[3],gerbilArray[4] 被创建

But as you are trying to access the gerbilArray[5], it is showing NullPointerException.但是当您尝试访问 gerbilArray[5] 时,它显示 NullPointerException。 You have used "i<=numberofGerbils" .您已经使用了"i<=numberofGerbils" it should not be "<=" .不应该是 "<=" It should be "<" only.它应该只是“<” This will solve the problem.As when you are using "=", the array index 5 is called, which actually does not exists!!!这样问题就解决了。当你用“=”的时候,调用的是数组索引5,实际上是不存在的!!!

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

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