简体   繁体   English

为什么我在在线编码 class 代码检查器上收到“线程中的异常”主“java.util.NoSuchElementException”错误?

[英]Why am I getting a “Exception in thread ”main“ java.util.NoSuchElementException” error on a online coding class code checker?

Every time I run the code on BlueJ, Vscode, and/or Atom it works perfectly fine.每次我在 BlueJ、Vscode 和/或 Atom 上运行代码时,它都能正常工作。 However it never works on the code checker.但是它永远不会在代码检查器上工作。

 import java.util.Scanner;
    import java.lang.Math;

    class Lesson_20_Activity {
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        
        double fNorth = 0;
        
        double input = 0;
        
        double fSouth = 0;
        
        double fEast = 0;
        
        double fWest = 0;
        
        int cont = 1;
        
        double input2 = 0;
        
        while(cont == 1 ){
            
            System.out.println("Please enter the latitude: ");
            input = scan.nextDouble();
                 
                if(input >=-90.0 && input <= 90.0){
                
                    if(fNorth == 0.0){
                    
                    fNorth = input;
                    
                    fSouth = input;
                    
                  }
                  else{
                    
                    if(input<fSouth){
                        fSouth = input;
                    }
                    else if (input>fNorth){
                        
                        fNorth = input;
                        
                    }          
                 }
                
             }
            
            System.out.println("Please enter the longitude");
            input2 = scan.nextDouble();
            
            if(input2 >= -180.0 && input2 <= 180.0){
                
                if(fWest == 0){
                    
                    fWest = input2;
                    
                    fEast =  input2;
                    
                }
                else{
                    
                    if(input2<fWest){
                        
                        fWest = input2;
                        
                    }
                    
                    else if(input2>fEast){
                        fEast = input2;
                    }
                }
                
            }
            else{
                
                System.out.println("Please enter the longitude:");
            }
            System.out.println("Would you like to enter another location?");
            cont = scan.nextInt();
            
            if(cont == 0){   
                System.out.println("Farthest North: " + fNorth);
            
                System.out.println("Farthest South: " + fSouth);
            
                System.out.println("Farthest East: " + fEast);
            
                System.out.println("Farthest West: " + fWest);
            }
        }

    /*
     * Write your code here Copy and paste your entire program to Code Runner to
     * complete the activity, from the first import statement to the last bracket.
     */
  }
  }

Here's the error that appears:这是出现的错误:

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.nextDouble(Scanner.java:2413)
        at Lesson_20_Activity.main(Main.java:380)
        at Ideone.assertRegex(Main.java:85)
        at Ideone.assertRegex(Main.java:76)
        at Ideone.test(Main.java:40)
        at Ideone.main(Main.java:29)

I don't know what else to do now.我不知道现在还能做什么。 I really need help to resolve this.我真的需要帮助来解决这个问题。

In your code, you invoke scan.nextDouble() twice in a row, and thus the line separator is never consumed from the input stream.在您的代码中,您连续两次调用scan.nextDouble() ,因此从输入 stream 中永远不会消耗行分隔符。 The first call reads a double, but then the second call looks at the input stream, and since the line separator is still there, but the actual contents of the line was already consumed, it just returns an empty string, which can't be parsed as a double.第一次调用读取一个双精度,但随后第二次调用查看输入 stream,由于行分隔符仍然存在,但该行的实际内容已经被消耗,它只是返回一个空字符串,不能解析为双精度。 To solve this problem, just invoke scan.nextLine() in between any calls to scan.nextDouble() or scan.nextInt() .要解决此问题,只需在对scan.nextDouble()scan.nextInt() scan.nextLine()任何调用之间调用 scan.nextLine() 即可。 This will consume the line separator characters from the input stream, and allow the scanner to "see" the next line.这将消耗来自输入 stream 的行分隔符,并允许扫描仪“看到”下一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取关于线程“main”中的异常的错误 java.util.NoSuchElementException - Getting Error Regarding Exception in thread "main" java.util.NoSuchElementException 我一直得到“线程中的异常”主“java.util.NoSuchElementException” - I keep getting “Exception in thread ”main“ java.util.NoSuchElementException” 我执行以下代码时,发生“ main”线程中的“ java.util.NoSuchElementException”异常。 - “ Exception in thread ”main“ java.util.NoSuchElementException ” error is occured when i execute the following code. 编译此代码时,我收到错误“线程“ main” java.util.NoSuchElementException中的异常” - I receive the error “Exception in thread ”main“ java.util.NoSuchElementException” upon compiling this code 继续获取:线程“main”java.util.NoSuchElementException中的异常 - keep getting: Exception in thread “main” java.util.NoSuchElementException Java扫描器错误:线程“主”中的异常java.util.NoSuchElementException - Java Scanner Error: Exception in thread “main” java.util.NoSuchElementException 线程“主”中的Java异常java.util.NoSuchElementException运行时错误 - Java Exception in thread “main” java.util.NoSuchElementException runtime error 我的tokenizer类中的线程“ main”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException in my tokenizer class 线程“主”中的异常java.util.NoSuchElementException? - Exception in thread “main” java.util.NoSuchElementException? 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM