首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Java.util.scanner时noSuchElement出错

使用Java.util.scanner时noSuchElement出错
EN

Stack Overflow用户
提问于 2018-10-21 00:21:05
回答 1查看 137关注 0票数 0

错误

在线程“

”java.util.NoSuchElementException中出现异常:在CircleFormulas.main(scanner.java:22)的主线程中找不到任何行

My code

import java.util.Scanner;
public class CircleFormulas {

    public static void main(String[] args) {
        //Creates my constructors/variables
        //Scans for the user's name
        Scanner userName1 = new Scanner (System.in);
        //Scans for the radius the user wishes to calculate
        Scanner user_Radius = new Scanner (System.in);


        //Asks the user for their name and places their response into the userName variable
        System.out.println("What is your name?: ");
        String userName = userName1.nextLine();
        //closes the line function; locks the variable
        userName1.close();
        //Prints out a greeting for the user
        System.out.println("Hey " + userName + " How are you?");
        //Asks the user a question
        System.out.println("Now, what is the radius of the circle you'd like to calculate?");
        //Asks the user for their radius they'd like to calculate and places their response into the radius variable
        String radius = user_Radius.nextLine();
        user_Radius.close();
        //Prints out the radius of the user's circle
        System.out.println("So, the radius of your circle is: " + radius);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-21 00:29:43

出现错误是因为您试图重用关闭的System.in (即使它在另一个变量中声明,但仍然是关闭的)。

您不需要实例化多个扫描器,只需执行一次并多次重用就足够了:

Scanner scanner = new Scanner(System.in);

String userName = scanner.nextLine();
String radius = scanner.nextLine();

scanner.close();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52907697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档