+ 1

What is the error in this code and how to correct it?

How to solve this error. check my codes for code. import java.util.*; public class Program { public static void main(String[] args) { ArrayList<String> a=new ArrayList<String>(); a.add("hdjdj"); a.add("eudu"); a.add("ejxj"); Iterator it=a.iterator(); while(it.hasNext()) { System .out.print(it.Next()+""); } } }

2nd Apr 2017, 11:15 AM
Manideep Yadav
Manideep Yadav - avatar
9 Answers
+ 2
Your code has one error calling iterator method "next()" inside while loop... In next() method all the characters should be in lower case.. - One more improvement should be provide some spacing after printing list elements.. you have just appended empty string it should be space.. I have modified ur code please refere and execute it.. ----------------------------------------- import java.util.*; public class Program { public static void main(String[] args) { ArrayList<String> a=new ArrayList<String>(); a.add("hdjdj"); a.add("eudu"); a.add("ejxj"); Iterator it=a.iterator(); while(it.hasNext()) { System .out.print(it.next()+" "); } } } ----------------------------------------- Hope it helps, Happy Programming ;)
2nd Apr 2017, 11:42 AM
Sbk0103
Sbk0103 - avatar
+ 7
Inside the while loop, System.out.println(it.next()+""); is correct.
2nd Apr 2017, 11:38 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 1
thanks sir@krishna got it now😃
2nd Apr 2017, 11:42 AM
Manideep Yadav
Manideep Yadav - avatar
+ 1
Thank you 😃@sbk I was about to ask the difference between Next() and next().and thanks for the advice👍
2nd Apr 2017, 11:46 AM
Manideep Yadav
Manideep Yadav - avatar
+ 1
@Manideep, In Iterator interface there is no such method named Next(). so there is no difference between the two.. Ideally every method provided by java API starts with lower case.. Only class names and interface names starts with upper case.. Thanks :)
2nd Apr 2017, 11:54 AM
Sbk0103
Sbk0103 - avatar
+ 1
java API? Interface ??no idea! still have a long way to go😅.Thanks again@sbk
2nd Apr 2017, 11:59 AM
Manideep Yadav
Manideep Yadav - avatar
+ 1
No kidding 😅@sbk. I am only still half way through java course in sololearn, and still there's a lot more to learn form outside😃.
2nd Apr 2017, 12:15 PM
Manideep Yadav
Manideep Yadav - avatar
0
@Manideep I guess u r kidding
2nd Apr 2017, 12:08 PM
Sbk0103
Sbk0103 - avatar
0
Haha ok then.. Good luck 😄👍
2nd Apr 2017, 12:26 PM
Sbk0103
Sbk0103 - avatar