Click here to Skip to main content
15,886,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import java.lang.*;
import java.util.Scanner;

class ArrObj
{
	public int rollno,clss;
	public String name,section;
	Scanner s = new Scanner(System.in);
	
	public void getdata()
	{
		System.out.println("ENTER NAME");
		name = s.next();
		System.out.println("ENTER CLASS");
		clss = s.nextInt();
		System.out.println("ENTER Section");
		section = s.next();
		System.out.println("ENTER ROLL NO");
		rollno = s.nextInt();
	}
	
	public void putdata()
	{
		System.out.println("THE NAME IS : "+name+" THE CLASS IS : "+clss+" THE SECTION IS : "+section+" THE ROLL NUMBER IS :"+rollno);
	}

}

public class mycode
{
	public static void main(String args [])
	{
		int x,i;
		Scanner t = new Scanner(System.in);
		
		
		System.out.println("ENTER THE SIZE");
		
		x=t.nextInt();
		
		ArrObj[] itr = new ArrObj[x];
		
		System.out.println("ENTER THE DETAILS :");
		
		for(i =0;i<x;i++)
		{
			itr[i] = new ArrObj();
			itr[i].getdata();
		}
		
		for(i=0;i<x;i++)
		{
			itr[i] = new ArrObj();
			itr[i].putdata();
		}
	
	}
	
}


THE OUTPUT I GET IS :
THE NAME IS : null THE CLASS IS : 0 THE SECTION IS : null THE ROLL NUMBER IS :0


What I have tried:

i dono what to do please help me. thank you
Posted
Updated 5-Jul-19 21:17pm

1 solution

Look at your code:
Java
for(i=0;i<x;i++)
{
    itr[i] = new ArrObj();
    itr[i].putdata();
}
Each time round the loop, you create a brand new empty object and overwrite the existing one. Then you try to print it's - empty - content, and surprised that its ... well ... empty.

Remove the line that creates a new object and overwrites the array element from that loop, and see what happens.
 
Share this answer
 
Comments
SAI TEJ99 6-Jul-19 3:21am    
YES IT WORKED THANKYOU :D
OriginalGriff 6-Jul-19 3:28am    
You're welcome!

(But DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.)

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