Click here to Skip to main content
15,504,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
package zen;
import java.util.*;
public class zen1
{
	public int id;
	public String name;
	public String email;
	public zen1(int id,String name,String email)
	{
		this.id=id;
		this.name=name;
		this.email=email;
	}
	public static void ze(zen1 s[])
	
	{
	
		for(zen1 t:s)
		{
			System.out.println(t.id+" "+t.name+" "+t.email);
		}
	}
	
	public static void main(String args[])
	{
		
		zen1[] s=new zen1[3];

		Scanner test=new Scanner(System.in);
		for(int i=0;i<s.length;i++)
		{
			s[i]=new zen1(test.nextInt(),test.nextLine(),test.nextLine());
			ze(s[i]);
		
		}
	}
}

What I have tried:

i am getting error in ze(s[i]) if i passed ze method as array type i can't able to access it showing error
Posted
Updated 20-Jul-18 0:21am

as per
Java
public static void ze(zen1 s[])


You need to call it as ze(s);
If you want to pass single element, modify your ze method to

Java
public static void ze(zen1 s)


but that require implementation changes as well in side function.
 
Share this answer
 
Comments
Member 13900790 20-Jul-18 7:54am    
if i use public static void ze(zen1 s) i can't able to access the array values in for-each loop.
Manish K. Agarwal 23-Jul-18 0:37am    
then declare it as public static void ze(zen1 s[]) and modify your logic
That's because you declared ze as accepting a parameter that is an array of zen1 items:
public static void ze(zen1 s[])
And you are trying to pass a single zen1 instance to the method:
s[i]=new zen1(test.nextInt(),test.nextLine(),test.nextLine());
ze(s[i]);
Change the method definition to accept a single instance, or call the method outside the loop and pass the whole array:
for(int i=0;i<s.length;i++)
	{
		s[i]=new zen1(test.nextInt(),test.nextLine(),test.nextLine());
	}
ze(s);
You do not want to pass the whole array inside the loop because you do not have all elements containing actual instances until the loop is complete.
 
Share this answer
 
v3
Comments
Member 13900790 20-Jul-18 7:27am    
can you please code that particular area and how it works
OriginalGriff 20-Jul-18 7:36am    
No, because this is your homework, and we have given you enough information to do it on your own.
Member 13900790 20-Jul-18 8:10am    
thanks for clarifying my doubt.and i know now how it works

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