Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
public class dublicate 
{ 
	public static void isDuplicate(Character arr)
	{   int len=0;
		while(arr!=null)
		len++;
		Set<character> set=new HashSet<character>(Arrays.asList(arr));
		int len2=set.size();
		if(len==len2)
			System.out.println("array doesn't have duplicates");
		else
			System.out.println("yes they have duplicates\n");
		
	}
	public static void main(String args[])
	{
Scanner s=new Scanner(System.in);
System.out.println("enter elements in an array to check it contains duplicate elements or not\n");
Character arr[]=new Character[100];
for(Character c:arr)
	arr[c]=s.next().charAt(0);
}
}


What I have tried:

Here I'm getting NullPointer exception.I'm not getting how to correct it.Can anyone please help me?
Posted
Updated 7-Jan-19 19:52pm
v2
Comments
Richard MacCutchan 26-Aug-18 4:45am    
You fail to tell us where the exception occurs. However the two lines:

for(Character c:arr)
arr[c]=s.next().charAt(0);

make no sense. You are trying to use an object reference as an index into an array.

1 solution

You could have used split method in java to take the input from the console.
Something like
Java
split(" ");
 
Share this answer
 

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