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)
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.
0
what is ur error?
0
i edited the post.
0
try nextLine. If I saw your add() method then maybe I could say what is wrong.
0
thanks for the anwsers! :)