gpt4 book ai didi

java - 最终打印语句返回 null (java)

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:11 25 4
gpt4 key购买 nike

我有一个基本的名称应用程序,它从主类中获取用户数据,在解析器类中拆分数据,然后尝试分配最终类中的所有内容并在 toString 方法中将其打印出来。我知道主类和解析器工作正常。我已经在解析器类中验证了数据确实正确分割,并且还通过我创建的对象将数据发送到最终类以将其全部分配。但是,我的最终代码返回 null..

主类

<小时/>
            import java.util.Scanner;
public class MainClass {
public static void main (String[]args)
{
Scanner input = new Scanner(System.in); //create scanner object to gather name information
String fullName = null; //set the predefined value for the users name to null
nameParse splitInformation = new nameParse(); //method build to split the name into different sections
SecondClass access = new SecondClass(); //class built to output the different name data

System.out.println("What is your name?");

fullName = input.nextLine(); //store the users name and pass it into the data parser

splitInformation.parseNameInformation(fullName); //name parsing parameters built

System.out.println(access.toString());





}

}

数据解析器类

<小时/>
            public class nameParse {
private String firstName;
private String middleName;
private String lastName;

public nameParse()
{
firstName = "initial";
middleName = "initial";
lastName = "initial";
}

public void parseNameInformation(String inputInfo)
{

//Create an array to store the data and split it into multiple sectors
String nameInformation[] = inputInfo.split("\\s");

firstName = nameInformation[0];
middleName = nameInformation[1];
lastName = nameInformation[2];

//System.out.println(firstName + " " + middleName + " " + lastName);

SecondClass sendData = new SecondClass();

sendData.setFirstName(firstName);
sendData.setMiddleName(middleName);
sendData.setLastName(lastName);


}

}

最后一个类__

    public class SecondClass {

private String firstName;
private String middleName;
private String lastName;


/*public String GFN()
{
return firstName;
}

public String GMN()
{
return middleName;
}

public String GLN()
{
return lastName;
}*/
public String setFirstName(String yourFirstName)
{
firstName = yourFirstName;
return this.firstName;
}

public String setMiddleName(String yourMiddleName)
{
middleName = yourMiddleName;
return this.middleName;
}

public String setLastName(String yourLastName)
{
lastName = yourLastName;
return this.lastName;
}

public String getFN()
{
return firstName;
}

public String toString()
{
String printNameInfo = "\nYour First Name:\t" + getFN();
return printNameInfo;
}

}

最佳答案

您从未设置任何 SecondClass 对象的(称为“访问”)字段,因此它们当然都将为空。

简而言之,您的代码创建一个 nameParse 对象,从用户获取信息,但对该信息不执行任何操作。您创建了一个名为 access 的 SecondClass 对象,没有向其中放入任何数据,因此当您尝试打印它时,应该期望其中没有有效数据。解决方案:首先将信息放入 SecondClass 对象中。调用它的setter方法:

// be sure to call the setter methods before trying to print anything out:
access.setSomething(something);
access.setSomethingElse(somethingElse);
<小时/>

编辑
您声明:

I thought I set the data using the sendData.setFirstname(...) etc?

parseNameInformation 方法中,您创建了一个新的 SecondClass 对象,并设置了该对象的字段,但该对象与 main 方法中的字段仍然为 null 的对象完全不同。要解决此问题,请为 parseNameInformation 提供一个方法参数,并将主方法的 SecondClass 对象传入其中并设置其方法。当然,您必须在调用方法之前创建 SecondClass 对象。

即,

public void parseNameInformation(String inputInfo, SecondClass sendData)
{
//Create an array to store the data and split it into multiple sectors
String nameInformation[] = inputInfo.split("\\s");

firstName = nameInformation[0];
middleName = nameInformation[1];
lastName = nameInformation[2];

//System.out.println(firstName + " " + middleName + " " + lastName);

// SecondClass sendData = new SecondClass(); // !!! get rid of this

sendData.setFirstName(firstName);
sendData.setMiddleName(middleName);
sendData.setLastName(lastName);
}

关于java - 最终打印语句返回 null (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21651274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com