gpt4 book ai didi

java - 如何删除/不显示空值?

转载 作者:行者123 更新时间:2023-12-02 11:12:26 25 4
gpt4 key购买 nike

所以我的问题是我如何删除/不显示空值、未找到的多余值以及运行程序时的 0,以及“运行”时 Student201 的最后两个属性,它以相反的顺序显示,这意味着首先显示空值或未找到的多余值或零。

另外我如何添加学生“预定义”

This is my preferred output or display as much as possible w/o the not found

This is not what I want, the not founds are printed first before the desired output

Main.java

public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub

//Student a1=new Student();
//a1.choosy();

Student201 a2 =new Student201();
a2.studinfo();
a2.findstud("2000000001);
}

}

Student201.java

public class Student201 {

Student [] studarray = new Student[13];
int x;
public void studinfo()
{



for (x=0;x<studarray.length;x++) {

studarray[x]= new Student();

}


Student estudyante1 = new Student();
Student estudyante2 = new Student ();
Student estudyante3 = new Student ();
Student estudyante4 = new Student ();
Student estudyante5 = new Student ();

estudyante1.getStudName("Yves Francisco");
estudyante1.getStudNum(2000000001);
estudyante1.getYrLvl(5);
estudyante1.getKors("CpE");
estudyante1.getGender("Male");

estudyante2.getStudName("Lance Eco");
estudyante2.getStudNum(2000000002);
estudyante2.getYrLvl(5);
estudyante2.getKors("CpE");
estudyante2.getGender("Male");

estudyante3.getStudName("Karlos Castillo");
estudyante3.getStudNum(2000000003);
estudyante3.getYrLvl(5);
estudyante3.getKors("CpE");
estudyante3.getGender("Male");

estudyante4.getStudName("Glenn Bordonada");
estudyante4.getStudNum(2000000004);
estudyante4.getYrLvl(4);
estudyante4.getKors("ECE");
estudyante4.getGender("Male");

estudyante5.getStudName("Tim Tolentino");
estudyante5.getStudNum(2000000005);
estudyante5.getYrLvl(4);
estudyante5.getKors("ECE");
estudyante5.getGender("Male");

studarray[0]=estudyante1;
studarray[1]=estudyante2;
studarray[2]=estudyante3;
studarray[3]=estudyante4;
studarray[4]=estudyante5;


}
public void findstud (String query) //String query for searching

{

int ercatch=0;

try{

ercatch=Integer.parseInt(query);
}

catch (NumberFormatException m)
{


}

for (x=0;x<studarray.length;x++)
{

if (query.equalsIgnoreCase(studarray[x].setStudName())) //query.equalsIgnorecase for case sensitive inputs
{
System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender()+"\n");
System.out.println("\n");
}




}

for (x=0;x<studarray.length;x++)
{
if (ercatch == studarray[x].setStudNum()) //Integer.parseInt for int data types
{

System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}

else if (ercatch != studarray[x].setStudNum())
{

System.out.println("Not Found!");
}


}

for (x=0;x<studarray.length;x++)
{

if (ercatch == studarray[x].setYrLvl())
{
System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}

else if (ercatch != studarray[x].setYrLvl())
{
System.out.println("Not Found!");
}

}

for (x=0;x<studarray.length;x++)
{

if (query.equalsIgnoreCase(studarray[x].setKors()))
{

System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}


}

for (x=0;x<studarray.length;x++)
{
if (query.equalsIgnoreCase(studarray[x].setGender()))
{

System.out.println(studarray[x].setStudName()+"\n"+studarray[x].setStudNum()+"\n"+studarray[x].setYrLvl()+"\n"+studarray[x].setKors()+"\n"+studarray[x].setGender());
System.out.println("\n");
}


}



}

public void addstud (String query)

{


}

}

学生.Java

public class Student {


private String StudName;
private int StudNum;
private int YrLvl;
private String Kors;
private String Gender;

//this just for naming convention for the get and set
public void getStudName (String name) {

this.StudName=name;
}


public String setStudName() {

return StudName;
}

public void getStudNum (int numero) {

this.StudNum=numero;
}

public int setStudNum() {

return StudNum;
}

public void getYrLvl (int yrlvl) {

this.YrLvl=yrlvl;
}

public int setYrLvl()
{
return YrLvl;
}

public void getKors (String korse) {

this.Kors=korse;
}

public String setKors() {

return Kors;
}

public void getGender (String sex)
{
this.Gender=sex;
}

public String setGender() {

return Gender;
}

public void choosy() {

System.out.println("Here is the list and the information of the Students \n");


}

/*public static void main(String[] args) {
// TODO Auto-generated method stub

}*/

}

最佳答案

首先,你完全混淆了 getterssettersgetters 应该返回不带任何参数的值,而 setters 使用参数将字段设置为值,如下所示

public void setStudName (String name) {
this.StudName=name;
}

public String getStudName() {
return StudName;
}

public void setStudNum (int numero) {
this.StudNum=numero;
}

public int getStudNum() {
return StudNum;
}

由于您不想显示“未找到”结果,因此我将其删除,这是您的 Student201 类。

public class Student201 {

Student[] studarray = new Student[13];
int x;

public void studinfo() {

for (x = 0; x < studarray.length; x++) {

studarray[x] = new Student();

}

Student estudyante1 = new Student();
Student estudyante2 = new Student();
Student estudyante3 = new Student();
Student estudyante4 = new Student();
Student estudyante5 = new Student();

estudyante1.setStudName("Yves Francisco");
estudyante1.setStudNum(2000000001);
estudyante1.setYrLvl(5);
estudyante1.setKors("CpE");
estudyante1.setGender("Male");

estudyante2.setStudName("Lance Eco");
estudyante2.setStudNum(2000000002);
estudyante2.setYrLvl(5);
estudyante2.setKors("CpE");
estudyante2.setGender("Male");

estudyante3.setStudName("Karlos Castillo");
estudyante3.setStudNum(2000000003);
estudyante3.setYrLvl(5);
estudyante3.setKors("CpE");
estudyante3.setGender("Male");

estudyante4.setStudName("Glenn Bordonada");
estudyante4.setStudNum(2000000004);
estudyante4.setYrLvl(4);
estudyante4.setKors("ECE");
estudyante4.setGender("Male");

estudyante5.setStudName("Tim Tolentino");
estudyante5.setStudNum(2000000005);
estudyante5.setYrLvl(4);
estudyante5.setKors("ECE");
estudyante5.setGender("Male");

studarray[0] = estudyante1;
studarray[1] = estudyante2;
studarray[2] = estudyante3;
studarray[3] = estudyante4;
studarray[4] = estudyante5;

}

public void findstud(String query) //String query for searching
{

int ercatch = 0;

try {
ercatch = Integer.parseInt(query);
} catch (NumberFormatException m) {
}

for (x = 0; x < studarray.length; x++) {

if (query.equalsIgnoreCase(studarray[x].getStudName())) //query.equalsIgnorecase for case sensitive inputs
{
System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender() + "\n");
System.out.println("\n");
}

}

for (x = 0; x < studarray.length; x++) {
if (ercatch == studarray[x].getStudNum()) //Integer.parseInt for int data types
{

System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}

}

for (x = 0; x < studarray.length; x++) {

if (ercatch == studarray[x].getYrLvl()) {
System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}

}

for (x = 0; x < studarray.length; x++) {

if (query.equalsIgnoreCase(studarray[x].getKors())) {

System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}

}

for (x = 0; x < studarray.length; x++) {
if (query.equalsIgnoreCase(studarray[x].getGender())) {

System.out.println(studarray[x].getStudName() + "\n" + studarray[x].getStudNum() + "\n" + studarray[x].getYrLvl() + "\n" + studarray[x].getKors() + "\n" + studarray[x].getGender());
System.out.println("\n");
}

}
}

public void addstud(String query) {

}

}

关于java - 如何删除/不显示空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50532747/

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