Java代码中的java.util.NoSuchElementException异常 [英] java.util.NoSuchElementException in Java code

查看:266
本文介绍了Java代码中的java.util.NoSuchElementException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这个例外:

 请提供宽度:4 
请提供height:线程中的异常mainjava.util.NoSuchElementException
在java.util.Scanner.throwFor(未知源)
在java.util.Scanner.next(未知来源)
at java.util.Scanner.nextDouble(Unknown Source)
at rr.fr.op.lab.prob1.Rectangle.scanner(Rectangle.java:51)
at rr.fr.op.lab.prob1我的代码是:。 >

 包rr.fr.op.lab.prob1; 
import java.util.Scanner;
public class Rectangle {

public static void main(String [] args){

if(args.length!= 2&& args.length != 0){
System.err.println(提供的参数数无效);
System.exit(1);

double a = Double.parseDouble(args [0]);
double b = Double.parseDouble(args [1]);

double area = area(a,b);
双周长=周长(a,b);

System.out.println(您指定了一个宽度为+ a +和高度
+ b +的矩形,其面积为+ area +及其周长是+周界)
}

double x,y,z;
if(args.length == 0){
System.out.printf(请提供width:);
x = scanner();
System.out.printf(请提供height:);
y = scanner();
}
}

private static double area(double a,double b){
return a * b;
}

私人静态双边界(double a,double b){
return 2 *(a + b);
}

private static double scanner(){
Scanner sc = new Scanner(System.in);
double number = sc.nextDouble();
sc.close();
返回号码;
}
}

之后,我想使用方法trim )删除空格。那可能吗?而且,我也需要isEmpty()方法。该代码必须计算矩形的面积和周长。输入是键盘或命令行。

解决方案

使用扫描仪后关闭扫描仪。在这种情况下,这也关闭了底层流 System.in ,因为它实现了 Closeable



当您下次创建扫描仪时, System.in 已经关闭,因此您无法从中读取更多元素。 p>

创建一个扫描仪并重复使用多次。






被认为是关闭流的不良做法,如果您没有打开它(或者您可能会泄露参考)。



可能还有其他代码依赖于流被打开,所以关闭它可能导致该代码失败。远离原因的故障远远难以进行调试。



不幸的是,无意中关闭流是非常容易的,因为Scanner,BufferedInputStream等类可以关闭其底层流关闭时。


I get this exception:

Please provide width: 4
Please provide height: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at rr.fr.op.lab.prob1.Rectangle.scanner(Rectangle.java:51)
    at rr.fr.op.lab.prob1.Rectangle.main(Rectangle.java:31)

My code is:

package rr.fr.op.lab.prob1;
import java.util.Scanner;
public class Rectangle {

    public static void main(String[] args) {            

        if(args.length != 2 && args.length != 0){
            System.err.println("Invalid number of arguments was provided.");
            System.exit(1);

            double a = Double.parseDouble(args[0]);
            double b = Double.parseDouble(args[1]);

            double area = area(a,b);
            double perimeter = perimeter(a,b);

            System.out.println("You have specified a rectangle of width " + a + " and height "
                + b + ". Its area is " + area + " and its perimeter is " + perimeter);
        }

        double x,y,z;
        if(args.length == 0){
            System.out.printf("Please provide width: ");
            x = scanner();
            System.out.printf("Please provide height: ");
            y = scanner();
        }
    }

    private static double area(double a, double b){
        return a*b;
    }

    private static double perimeter(double a, double b){
            return 2*(a+b);
    }

    private static double scanner (){
        Scanner sc = new Scanner (System.in);
        double number = sc.nextDouble();
        sc.close();
        return number;  
    }
}

After that, I would like to use method trim() to delete whitespaces. Is that possible? And, I need isEmpty() method too. This code must calculate area and perimeter of rectangle. Inputs are form keyboar or command line.

解决方案

You close the scanner after you use it. This also closes the underlying stream, System.in in this case, since it implements Closeable.

When you next create a scanner, System.in is already closed, so you can't read more elements from it.

Create a single scanner and reuse it multiple times.


It is considered a bad practice to close a stream if you didn't open it (or you have potentially leaked the reference).

There may be other code which relies upon the stream being open, so you closing it can lead to failures in that code. Failures remote from the cause like that are notoriously difficult to debug.

Unfortunately, it is very easy to close streams unintentionally, since classes like Scanner, BufferedInputStream etc close their underlying stream when they are closed.

这篇关于Java代码中的java.util.NoSuchElementException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆