0

** as you can see I have made a program to print Student Record of same branch and college in which user inputs the name and roll number.

I have tried generating toString() from my IDE but it is still showing " [Ljava.lang.String;@4e50df2e" these...please help me get rid of this...thank You.**


class Stur
{
static String branchcode="C04";
static String branch="Computer Sc. & Eng. ";
static String rollcode="18020";
static String collegeName="Kalaniketan Polytechnic College";

String rollno[],name[];

Stur(String n[],String r[])
{
rollno=r;
name=n;


System.out.println(name+" \t "+rollcode+""+branchcode+""+rollno+" \t"+branch+""+collegeName);

}
}   



class staticVar1    //main method class
{
public static void main(String ujyg[])
 
{
int j=0, i=0;

Scanner object=new Scanner(System.in);

System.out.println("how many Students ");
int n=object.nextInt();
String arr[]=new String [n];


for(i=0;i<arr.length;i++)
{
    System.out.print("Enter roll number:");
    arr[i]=object.next();

    System.out.print("Enter Name:");
    arr[i]=object.next();
}

System.out.println("Name \t Roll Number \t   Branch \t\t College\n");


for(j=0;j<arr.length;j++)
{
Stur st=new Stur(new String[i], new String[i]);      

}}
}

Expecting output like this

Name    Roll Number     Branch    College

Anf   18020C0406     Computer Sc. & Eng. Kalaniketan Polytechnic College 
xyz   18020C0402     Computer Sc. & Eng. Kalaniketan Polytechnic College
Ad    18020C0405     Computer Sc. & Eng. Kalaniketan Polytechnic College 
Ax    18020C0401     Computer Sc. & Eng. Kalaniketan Polytechnic College 
AnsafGhani
  • 13
  • 4

1 Answers1

1

Please find updated code.


class Stur
{
  static String branchcode="C04";
  static String branch="Computer Sc. & Eng. ";
  static String rollcode="18020";
  static String collegeName="Kalaniketan Polytechnic College";

  String rollno,name;

  Stur(String n,String r)
  {
    rollno=r;
    name=n;
    System.out.println(name + "\t" + rollcode+branchcode+rollno + "\t" + branch + " " + collegeName);
  }
}   

class staticVar1    //main method class
{
  public static void main(String ujyg[]) 
  {
    int j=0, i=0;

    Scanner object=new Scanner(System.in);

    System.out.println("how many Students ");
    int n=object.nextInt();
    String nameArr[]=new String [n];
    String rollnoArr[]=new String [n];


    for(i=0;i<n;i++)
    {
      System.out.print("Enter roll number:");
      rollnoArr[i]=object.next();

      System.out.print("Enter Name:");
      nameArr[i]=object.next();
    }

    System.out.println("Name \t Roll Number \t   Branch \t\t College\n");

    Stur st[] = new Stur[n];
    for(j=0;j<n;j++)
    {
      st[j] = new Stur(nameArr[j], rollnoArr[j]);      

    }
  }
}

You were trying to print an array directly, so it was giving you object hash code and class name. If you want output as you have mentioned, you need to print the element of the array one by one.

gupta_hemant
  • 136
  • 7
  • and how do I have to initialize it?i tried=...it is printing "null" in place of object hashcode – AnsafGhani Sep 15 '20 at 05:38
  • also the Print statement is iterating the square times of any number I am giving...( if i Enter 4, it is printing " null 18020C04null Computer Sc. & Eng. Kalaniketan Polytechnic College"16 times. if entered 5 then 25 times and so on. – AnsafGhani Sep 15 '20 at 06:04
  • Yes, you are right, I checked your code. Do you want that Stur class' object will represent one student which contains name and roll no?? – gupta_hemant Sep 15 '20 at 06:15
  • st[j] = new Stur(nameArr[j], rollnoArr[j]); Canyou Also please explain me this part? how these values are printing in our print statement? iam not getting it. – AnsafGhani Sep 15 '20 at 07:15
  • is it llike tht? that values inside the argument list i.e. nameArr and RollnoArr are going into our Constructor arguments??\ and what do Stur st[] = new Stur[n] ; this do? – AnsafGhani Sep 15 '20 at 07:18
  • I have created an array that will hold the objects of class `Stur` in this statement `Stur st[] = new Stur[n];`. So for every value in `nameArr` and `rollnoArr`, we are creating a new object of class `Stur` and putting the reference of all these objects into the elements of array. – gupta_hemant Sep 15 '20 at 08:10
  • For printing of values, whenever we create an object, the constructor of that class is called, and in our case constructor of `Stur` class is responsible for printing the statement. Whenever we create an object, the constructor is called and the statement is printed. Hope you will get this, if you want anything else let me know. – gupta_hemant Sep 15 '20 at 08:13