Scanner scanner =new Scanner(System.in);
strings=new String[6];
for(int j=0;strings.length>j;j++){
strings[j]=scanner.nextLine();
}
for(int i=0;strings.length>i;i++){
for(int k=i+1;strings.length>k;k++){
if(strings[i]==strings[k]){
strings[k]=null;
strings[i]=null;
}
}
for(int i=0;strings.length>i;i++){
System.out.print(strings[i]+" ");
}
What is wrong with my code?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
31 December 2022, 14:55
- there's a closing curly bracket missing for your main loop (add it after strings[i]=null; )
- comparing objects is done using the equals method and not == (then your code probably will throw a NullPointerException... fix it with not invoking equals on null or maybe just change your loop layout)
try this array to see that == does not work in all cases
- there is a logical problem in your code.... try this to see it
for more info see here:
https://codegym.cc/de/help/17476
+1
Dulsara
31 December 2022, 15:07
Thank you very much.I will try again,using your instructions.
+1