java - 扫描仪 - 不允许在整数值后输入字符串值

标签 java

下面给出的程序用于使用扫描仪读取输入。我在这里面临的问题是,如果我尝试先给出整数值,那么接下来就不允许我给出字符串值。它会自动跳到下一行。任何帮助将不胜感激

import java.io.*;
import java.util.Scanner;
class Employee
{
    int empid,salary;
    String empname,designation;
    public static void main(String args[])
    {
    Employee bha=new Employee();
    bha.details();
    bha.display();

    }

    void details()
    {
    Scanner s=new Scanner(System.in);
    System.out.println("enter the empid");
    empid=s.nextInt();//i'm am able to give the value here
    System.out.println("enter the employee name");
    empname=s.nextLine();// automatically skips to the next line(unable to give value)
    System.out.println("enter the employee designation");
    designation=s.nextLine();
    System.out.println("enter the employee salary");
    salary=s.nextInt();
    }
    void display()
    {
    System.out.println("the employee id is"+empid);
    System.out.println("the employee name is"+empname);
    System.out.println("the employee designation 

is"+designation);
    System.out.println("the employee salary is"+salary);
    }


}

最佳答案

试试这个方法:

    System.out.println("enter the empid");
    Scanner s = new Scanner(System.in);
    empid = Integer.parseInt(s.next());
    System.out.println("enter the employee name");
    empname = s.next();
    System.out.println("enter the employee designation");
    designation = s.next();
    System.out.println("enter the employee salary");
    salary = Integer.parseInt(s.next());

关于java - 扫描仪 - 不允许在整数值后输入字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19699036/

相关文章:

c# - 了解在 C# 和 Java 中产生不同结果的字符串的显式转换

java - 我如何在android中添加两个没有指数的 double 值

java - 我的应用程序 "find frequency of audio input from microphone"总是崩溃。但为什么?

java - 如何让 Java 中的 Spark 工作 - 无法初始化类 org.apache.spark.util.Utils$

java - 从 xml 中存储的数据创建文件

java - SSL环境下的SpringBoot+Eureka+CloudConfig

java - Neo4J POJO 方法

java - 如何在 Java 中创建带有连接按钮的 ButtonGroup?

java - Eclipse 在文件夹中找不到我的项目

java - 只读取 XML 中的根元素