1

I have created an Employee class in which I am only passing the value and printing the array. You can see:

package practicequestion;

import java.util.Scanner;
import practicequestion.employeeq.Employee;

public class EmployeeMain {
    public static void main(String[] args) {
        Employee[] obj = new Employee[3];
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of inputs you want ");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {

            System.out.println("Enter FirstName");
            String firstname = sc.nextLine();
            System.out.println("Enter LastName");
            String lastname = sc.nextLine();
            System.out.println("Enter Email");
            String email = sc.nextLine();
            System.out.println("Enter Salary");
            int salary = sc.nextInt();
            System.out.println("Enter Mobile Number");
            long mobileno = sc.nextLong();
            System.out.println("Enter Experience");
            int empexp = sc.nextInt();
            System.out.println("Enter Employee Id");
            int empid = sc.nextInt();
            if (i < n) {
                obj[i] = new Employee(firstname, lastname, email, salary, empid, empexp, mobileno);
            }
        }
        for (int i = 0; i < 3; i++) {
            System.out.println("Employee " + i +  " values are: ");
            obj[i].Display();
        }
        if (sc != null) {
            sc.close();   
        }
    }

}

But I am not able to input the firstname as whenever I run the program it starts input from last name. Its class is like this:

package practicequestion.employeeq;

public class Employee {
   String FirstName;
   String LastName;
   String Email;
   int Salary;
   long MobileNo;
   int EmployeeExp;
   int EmployeeId;

    public Employee(String FirstName,String LastName,String Email,int Salary,int EmployeeId,int EmployeeExp,long MobileNo) {
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Email = Email;
        this.Salary = Salary;
        this.EmployeeId = EmployeeId;
        this.EmployeeExp = EmployeeExp;
        this.MobileNo = MobileNo;
    }
    public void Display() {
        System.out.println(FirstName +"\n" + LastName + "\n" + Email + "\n" + Salary + "\n" + EmployeeId +"\n" + EmployeeId +"\n" + MobileNo);
    }
}

Can anyone solve this issue?

2 Answers2

1

This question was already answered here: Scanner is skipping nextLine() after using next() or nextFoo()?

Basically the problem is with Scanner.nextInt() which you call before entering the loop. Scanner.nextInt() only reads the next Integer the user inputs, not the full line, therefore when you call Scanner.nextLine() after it directly accepts the remaining (and empty) part of the line where you entered the integer.

To solve it, you either have to call Scanner.nextLine() once, or use Scanner.nextLine() to get the Integer and then cast it to use it as an Integer.

Moorts
  • 13
  • 3
  • Thanku for your suggestion. Now I am able to input it too BUT not able to print the first name. Please guide me how I can do so? – Shîvam Yadav Aug 27 '20 at 13:45
0

Try this. I will document where I made the changes.

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of inputs you want ");
int n = sc.nextInt();
// allocate the Employee array after you get the number of inputs.
Employee[] obj = new Employee[n];

for (int i = 0; i < n; i++) {

    // get the newline out of the buffer here. That also
    // takes care of the `nextInt` problem at the end of this loop
    sc.nextLine();
    
    System.out.println("Enter FirstName");
    String firstname = sc.nextLine();
    System.out.println("Enter LastName");
    String lastname = sc.nextLine();
    System.out.println("Enter Email");
    String email = sc.nextLine();
    System.out.println("Enter Salary");
    int salary = sc.nextInt();
    System.out.println("Enter Mobile Number");
    long mobileno = sc.nextLong();
    System.out.println("Enter Experience");
    int empexp = sc.nextInt();
    System.out.println("Enter Employee Id");
    int empid = sc.nextInt();
    // you don't need to verify `i` here.  The for loop does that
    // for you
    obj[i] = new Employee(firstname, lastname, email, salary,
            empid, empexp, mobileno);
    
}

// use the number of inputs `n` to limit the loop
for (int i = 0; i < n; i++) {
    System.out.println("Employee " + i + " values are: ");
    obj[i].Display();
}

And it is usually not a good practice to close the scanner.

WJS
  • 22,083
  • 3
  • 14
  • 32