1

The below given program is for reading inputs using scanner. The problem i am facing here is if i try to give an integer value first then it wont allow me to give string value next. it skips automatically to the next line. Any help will be appreciated

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);
    }


}
arjun
  • 23
  • 5

1 Answers1

0

Try this way:

    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());
Jhanvi
  • 4,744
  • 8
  • 30
  • 41