bugfix> java > 投稿

だから私の質問は、プログラムを実行するときにnull、余分なファウンド、0を削除/表示しない方法、また、「runed」のときにStudent201の最後の2つの属性、nullまたは過剰な未検出またはゼロが最初に表示されます。

また、「事前定義済み」の生徒を追加するにはどうすればよいですか

これは、見つからないものを除いて可能な限り私の好みの出力または表示です

これは私が望んでいるものではありません。目的の出力の前に、見つからないものが最初に印刷されます

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) 
    {

    }
}

Student.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
    }*/
}

回答 2 件
  • how can i Delete/not display the nulls,the excess not founds,and 0's

    まず、これを削除します

       for (x=0;x<studarray.length;x++) {
            studarray[x]= new Student();
        }
    
    

    次に、定義した生徒を除き、すべての生徒がnullになります。これらの生徒は明示的に無視できます。

    for (int x = 0; x < studarray.length; x++) {
        Student s = studarray[x];
        if (s == null) {
            continue; // here
        }
        // Check all your fields in a single loop
        if (query.equalsIgnoreCase(s.getStudName())) {
        } else if (Integer.parseInt(query) == s.getStudNum()) {
        } else {
            System.out.println("Student not found using query " + query);
        }
    }
    
    

  • まず最初に、あなたは完全に混乱していますゲッター そしてセッターゲッター パラメータなしで値を返す必要があり、セッター 次のように、パラメータを使用してフィールドを変数に設定します

    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) {
    }
    
    

    }

あなたの答え