0

error in code

import java.util.*; public class array { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); Scanner eyes = new Scanner(System.in); for(int i=0;i<5;i++){ // String asd = eyes.next(); list.add(eyes.next()); // System.out.println("asd"); } for(String s:list){ System.out.println(s); } } } this is my code, and i get error. error: Exception in thread "Main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at array.Main(array.java:11) what's the problem? (sorry for my bad english)

30th Jul 2016, 6:14 PM
Áron Szilågyi
Áron Szilågyi - avatar
5 Answers
+ 1
Not an error in code. Code is fine. Problem would be in the input. No element exception would mean a value isn't being added to the list. Since most compilers will ask for input each time it's called, I have to assume you're trying to run the code in code playground? If that's the case, you'll need to input all 5 values in the first input box. It doesn't run like a normal program and request input everytime scanner.next is called. It will take all values at once, and put them in accordingly.
31st Jul 2016, 2:16 AM
James
James - avatar
0
what is ur error?
30th Jul 2016, 6:48 PM
Suhail Pappu
Suhail Pappu - avatar
0
i edited the post.
30th Jul 2016, 7:10 PM
Áron Szilågyi
Áron Szilågyi - avatar
0
try nextLine. If I saw your add() method then maybe I could say what is wrong.
30th Jul 2016, 10:41 PM
Dmitry
Dmitry - avatar
0
thanks for the anwsers! :)
31st Jul 2016, 3:17 AM
Áron Szilågyi
Áron Szilågyi - avatar