线程“main”java.util.nosuchelementexception中出现异常:在java.util.scanner.nextline(scannerjava:1540)

h43kikqp  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(307)

以下代码产生以下错误: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) 你们中有谁能帮我找出导致错误的原因吗?问题出在哪里?

import java.util.*;
public class HelloWorld{

     public static void main(String []args){

      Scanner cin=new Scanner(System.in);
      System.out.println("Enter the name of the employee:");
      String name=cin.nextLine();
      System.out.println("Enter the age of the employee:");
      int age=cin.nextInt();
      System.out.println("Enter the salary of the employee:");
      Float sal=cin.nextFloat();
      System.out.println("Enter the year of joining of the employee(YYYY):");
      int doj=cin.nextInt();

      Calendar now=Calendar.getInstance();
      int year=Integer.parseInt(String.valueOf(now.get(Calendar.YEAR)));
      int joining=year - doj;
      System.out.println(joining);

      if(name.length()>=3&& name.length()<=20)
      {
          System.out.println("Name Valid");
      }
      else
      {
           System.out.println("Name Invalid");
      }

      if(age>=18 && age<=50)
      {
          System.out.println("Age Valid");
      }
      else
      {
           System.out.println("Age Invalid");
      }

      if(joining>25 || joining <0)
      {
          System.out.println("Valid Employee");
      }
      else
      {
           System.out.println("Invalid Employee");
      }
     }
}

我附上图片来澄清这个问题。
图像1图像2

tpxzln5u

tpxzln5u1#

你的代码对我有用:

Enter the name of the employee:
Fred
Enter the age of the employee:
20 1000 1968
Enter the salary of the employee:
Enter the year of joining of the employee(YYYY):
52
Name Valid
Age Valid
Valid Employee
x4shl7ld

x4shl7ld2#

查看您的代码和附加的图像,您期望使用scanner类的用户提供输入。提供输入 STDIN 旁边的 Source File 选项卡和代码应该可以工作。

相关问题