Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to create a method called registration in BtechStudent class and i have to read and store the data of students in an array of object and return.
finally i have to print the data of all students from array of object.
here is the code............

Java
import java.io.*;
class BtechStudent
{
    void registration()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter name");
        String name=br.readLine();
        System.out.println("branch");
        String branch=br.readLine();
        System.out.println("idNumber");
        String idNumber=br.readLine();
        //HOW TO RETURN THE OBJECT?????
    }
}

class DataBase
{
    public static void main(String args[])throws IOException
    {
        BtechStudent b[]=new BtechStudent[3];//I HAVE TAKEN 3 STUDENTS DATA..
        String name;
        String branch;
        String idNumber;
        for(int i=0;i<3;i++)
        {
            b[i].registration();
        }
        for(int j=0;j<3;j++)
        {
            System.out.println(b[j]);
        }
    }
}
//I WANT TO ENTER THE DATA OF STUDENT FROM registration METHOD NOT IN MAIN METHOD...AND //PRINT THE DETAILS OF STUDENTS MAIN METHOD....
//PLEASE HELP ME HOW TO DO.........
Posted
Updated 27-Aug-13 3:50am
v4
Comments
Maarten Kools 27-Aug-13 7:44am    
I think you'll need to look at your design, because it doesn't make sense to have the registration method return a value. Unless it's supposed to be static, and in that case just create the object? I don't really see why you're having a problem with this code. The error is pretty straightforward (either return a new BtechStudent instance (or this, but why would you want to do that?)) or make it return void.
[no name] 27-Aug-13 7:44am    
Return what object? You do not have a BtechStudent to return....

You seem to be a student, and you seem to have some misconceptions about how arrays and so forth. As such, I'll try to explain as best I can.

First off, in your main method you create an array of BtechStudent objects. When you create a array, you specify the size (in this case, you used 3). This creates "placeholders" for 3 BtechStudent objects. It does not create the objects. in your loop, when you do b[i].registration();, there is no object on which to invoke the registration method. To fix this problem, you need to create an object and place it in the array before you try to call the registration method. So, that changes your loop to:
Java
for(int i=0;i<3;i++)
{
    b[i] = new BtechStudent();
    b[i].registration();
}


Secondly, when you create your registration method, you specify that it will return a BtechStudent object, but it never returns anything. At the end of the method, you need to have a return statement. Since you are returning a BtechStudent object from the BtechStudent class, you will probably want to use return this; as your return statement. this is a special keyword that refers to the object on which you are currently executing a method.

That said, you should probably not return a BtechStudent object in that method anyway, since it would be redundant. Unless you have some reason for wanting to return a BtechStudent object, I suggest you change the method declaration to
Java
void registration() throws IOException

void means that the method does not return anything, so you can just call it to do some work or change the object in some way.

Next up is global variables. In your registration method, you capture some details and store it in local variables. This means that, as soon as the method ends, the variables are no longer accessible. Essentially, it means you asked the user to capture data and then you threw that data away. Instead, create some global variables in the BtechStudent class and store the captured data in them. This ensures that as long as you have the object, you have the data that the user captured. By now, your BtechStudent class looks like this:
Java
class BtechStudent
{
    public String name;
    public String branch;
    public String idNumber;

    void registration() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter name");
        name=br.readLine();
        System.out.println("branch");
        branch=br.readLine();
        System.out.println("idNumber");
        idNumber=br.readLine();
    }
}


Lastly, in your main method, you try to print the BtechStudent object. The problem is, it's a class you created, so Java has no idea how to print it. That will result in Java printing some useless crap instead of the name, branch and ID number you expect to see. In order to tell Java what to print when you try to print the object, you need to create a toString() method in your BtechStudent class. It should look something like this:
Java
public string toString()
{
    return "Name='" + name + "', Branch='" + branch + "', ID Number='" + idNumber + "'.";
}
 
Share this answer
 
Hello Vjnan,

The following code snippet should get you started.
Java
public class BTechStudent {
    private String _strName;
    private String _strBranch;
    private String _strId;

    public BTechStudent() {
    }

    public BTechStudent(String strName, String strBranch, String strId) {
        _strName = strName;
        _strBanch = strBranch;
        _strId = strId
    }

    public String getStudentName() {
        return _strName;
    }

    public String getBranch() {
        return _strBranch;
    }

    public String getId() {
        return _strId;
    }

    public void setStudentName(String pstrName) {
        _strName = pstrName;
    }
 
   public void setBranch(String pstrBranch) {
        _strBranch = pstrBranch;
    }
    public void setId(String pstrId) {
        _strId = pstrId;
    }
}

public class TestClass {
    public BTechStudent[] readStudentInfo() throws IOException {
        String strName = null;
        String strBranch = null;
        String strId = null;
        BTechStudent student = null;
        BufferedReader rdr = null;
        BTechStudent[] arrStudent = null;
 
        try {
            rdr = new BufferedReader(new InputStreamReader(System.in));
            arrStudent = new BTechStudent[3];
            for (int i = 0; i < 3; i++) {
                System.out.println("enter name");
                strName = rdr.readLine();
                System.out.println("branch");
                strBranch = br.readLine();
                System.out.println("idNumber");
                strId = br.readLine();
                student = new BTechStudent(strName, strBranch, strId);
                arrSrudent[i] = student;
            }
            return arrStudent;
        } finally {
            CleanUpUtils.doClose(rdr);
        }
    }

    public static void main(String[] args) throws IOException {
        TestClass tst = null;
        BTechStudent[] arrRet = null;

        tst = new TestClass();
        arrRet = tst.readStudentInfo();
        for (BTechStudent student : arrRet) {
             System.out.println("Student Name : " + student.getStudentName());
             System.out.println("Branch       : " + student.getBranch());
             System.out.println("Student Id   : " + student.getId());
             System.out.println("");
        }
    }
}

public abstract class CleanUpUtils {
    public static doClose(ICloseable toClose) {
        if (null == toClose) return;
        try {
            toClose.close();
        } catch (IOException ex) {
           // do nothing
        }
    }
}

Regards,
 
Share this answer
 
Comments
vjnan 27-Aug-13 9:17am    
Sir....I want to give the details of the student in the registration method only.....not in the main method and have to print the details of the student..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900