-1

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)
SirLacky
  • 45
  • 1
  • 5
  • Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Yoshikage Kira Sep 11 '19 at 16:59
  • You should probably check `scanner.hasNext()` before moving on and calling `scanner.nextInt()` or `scanner.nextLine()` – absolutelydevastated Sep 11 '19 at 17:00
  • 1
    Your first dummy `nextLine()` statement is unnecessary, since it is followed by a `nextInt()` call. The second `nextLine()` call is good, but you're **missing a `nextLine()` call at the end of the `for` loop**. – Andreas Sep 11 '19 at 17:02
  • 1
    **Don't create multiple `Scanner` objects wrapping `System.in`**. Use only one, and get rid of `scanIns` and `scanDelete`. – Andreas Sep 11 '19 at 17:04

1 Answers1

0

I just ran your code on my own installation. It worked. I also just ran it on the TutorialsPoint Java IDE and it worked as well.

Your code runs fine there as well when the input is done correctly, but the slightest error in input would cause the java.util.NoSuchElementException.

  1. Recheck your online inputs to be sure that they are correct. I put a space after Insert and it would jump to the end without letting me do the delete. It doesn't need to be an obvious input error.

  2. Another thing to consider is that an online IDE might be escaping characters to prevent malicious inputs. That could cause input problems.

Matthew Gaiser
  • 4,558
  • 1
  • 18
  • 35