Edit: My question was Why IntelliJ gives correct answer while website compiler gives Error.
I made my answer to one of coding exercises but it is not working in on website compiler. At a same time it's working in my IDE (IntelliJ). Exercise:
Input String:
5 //How many intigers to add to list
12 0 1 78 12 //Intigers to add
2 //How many commends to execute
Insert //Commend insert
5 23 //Add int 23 at index 5
Delete //Commend delete
0 //Which index to delete
Output String:
0 1 78 12 23 //Result String
My Answer:
int N = 0; //number of intigers
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt();
List <Integer> L = new ArrayList<>();
for (int i = 0; i < N; i++) {
L.add(scanner.nextInt());
}
scanner.nextLine();
int Q = 0; //number of commends
Q = scanner.nextInt();
String str = "";
scanner.nextLine();
for (int i = 0; i < Q; i++) {
str = scanner.nextLine();
if(str.equals("Insert")){
Scanner scanIns = new Scanner(System.in);
int indexToInsert = scanIns.nextInt();
int numberToInsert = scanIns.nextInt();
L.add(indexToInsert,numberToInsert);
}else if (str.equals("Delete")){
Scanner scanDelete = new Scanner(System.in);
int numToDel = scanDelete.nextInt();
L.remove(numToDel);
}
}
String formattedString = L.toString()
.replace(",", "")
.replace("[", "")
.replace("]", "")
.trim();
System.out.println(formattedString);
My answer is correct (the same as in question example) but still I am receiving Runtime Error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Solution.main(Solution.java:25)