NoSuchElementException when using tutorialspoint.com

安稳与你 提交于 2021-02-05 11:43:26

问题


So I was working on my code using this compiler site: https://www.tutorialspoint.com/compile_java_online.php

And when I ran my code it have me this 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 WorkingWithLoops.main(WorkingWithLoops.java:22)

This is my code:

import java.util.Scanner;

 public class WorkingWithLoops {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int negative = 0, positive = 0, sum = 0, value;
       
        System.out.print("Enter a value: ");
        value = input.nextInt();
       
        while (value != 0){
            if (value < 0){
                negative++;
            }
            else {
                positive++;
            }
           
            sum += value;
           
            System.out.print("\nEnter a value: ");
            value = input.nextInt();
           
        }
         System.out.print(
            "Positive integers: " + positive +
                "\nNegative integers: " + negative +
                "\nSum: " + sum +
                "\nAverage: " + ((sum * 1.0) /(positive/negative)) 
            );
       
           
    }
}

It prints out enter a value to accept the value but doesn't calculate or do anything. I'm not sure what part of my code is messing up. Im also kinda new to Java and I'm used to using C++.


回答1:


You need to provide input on the STDIN tab. For example:

1
-1
0

This is a problem specific to the tutorialspoint site you are using because it does not provide an interactive console. If you run the code locally, you will not get that error.




回答2:


I've never used this web compiler, but if I understand it correctly you'll have to specify the input (in the STDIN tab) before running the code and it will grab the input line by line.

As your code only end's if "0" is supplied the last line in STDIN has to be a zero. For example I tried the code with the following set of numbers:

10
1
-3
02
2300
0

Which gives the following output:

Enter a value: 
Enter a value: 
Enter a value: 
Enter a value: 
Enter a value: 
Enter a value: Positive integers: 4
Negative integers: 1
Sum: 2310
Average: 577.5

Additional info:

I don't know if it is recommendable for someone new to java to use such online compilers as I'm not sure about their reliability, but I'd rather recommend using an IDE such Eclipse or IntelliJ IDEA. In this case the IDE would actually allow you to input the numbers while the code is running instead of having to have it predefined.



来源:https://stackoverflow.com/questions/63873808/nosuchelementexception-when-using-tutorialspoint-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!