简体   繁体   English

扫描器类没有此类元素异常

[英]No such element exception with the scanner class

I'm a newbie to code and I'm trying to make a program that tells the user the temperature and how long to cook beef roasts. 我是一名新手,我正在尝试制作一个程序来告诉用户温度和煮牛肉烤的时间。 I decided to use scanner and now I'm getting the no such element exception error. 我决定使用扫描仪,现在遇到了没有此类元素异常的错误。 This is what my code looks like, and I can't tell what I'm doing wrong. 这就是我的代码的样子,我无法分辨自己做错了什么。

import java.util.Scanner;

public class Roasts {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Which Roast are you cooking?  1. Chuck 2. Tenderloin 3. Rib 4. Sirloin 5. Tri-tip 6. Shoulder ");

        int numb=0;

        numb=input.nextInt();

        switch (numb) {

            case 1:
            System.out.println("300 degrees for 3 hours");
            break;
            case 2:
            System.out.println("350 degrees for 1 hour, then turn off heat and leave roast in oven for 1 additional hour");
            break;
            case 3:
            System.out.println("375 degrees for 1 hour, then turn off heat and leave roast in oven for an additional 2 hours");
            break;
            case 4:
            System.out.println("350 degrees for 45 mins to one hour");
            break;
            case 5:
            System.out.println("375 degrees for 45 mins");
            break;
            case 6:
            System.out.println("325 degrees for 4 hours");
            break;
            default:
            System.out.println("invalid number please choose 1-6");
        }
    }
}

When NoSuchElementException is thrown (from here ): 当引发NoSuchElementException (从此处 )时:

Thrown by various accessor methods to indicate that the element being requested does not exist. 被各种访问器方法抛出以指示所请求的元素不存在。

When Scanner.nextInt() throws NoSuchElementException (from here ): Scanner.nextInt()抛出NoSuchElementException (从此处开始 )时:

If the input is exhausted 如果输入用尽

I actually don't see that your program that is crafted in an improper way, but it can be the case that you are supplying input that is causing this problem. 实际上,我看不到您的程序设计不当,但是您提供的输入可能会导致此问题。

Probably you should consider checking Scanner#hasNextInt() before invoking Scanner#nextInt() . 可能您应该在调用Scanner#nextInt()之前考虑检查Scanner#hasNextInt() Scanner#nextInt()

I'd also recommend you to first read javadocs of the classes that you are using to get better understanding of the behavior and make more sense of the errors/exceptions that you are getting as you are a beginner. 我还建议您先阅读所用类的javadocs,以更好地了解其行为,并更好地理解作为初学者时遇到的错误/异常。 HTH. HTH。

The possible reason for this (though seems strange in that particular situation) is that new line character is left in your scanner object. 可能的原因(尽管在特定情况下看起来很奇怪)是在扫描仪对象中保留了换行符。 Try to rework your line: 尝试重新制作您的线路:

numb=input.nextInt();

to: 至:

numb = Integer.parseInt(input.nextLine());

I tried running your code and it seems to me that there is no problem with it at all. 我尝试运行您的代码,在我看来,这根本没有问题。 Perhaps there is something wrong with your IDE? 也许您的IDE有问题?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM