Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在循环中输入第一个字符串,在java中它总是从第二个输入开始_Java_Arrays_Class_For Loop_Oop - Fatal编程技术网

无法在循环中输入第一个字符串,在java中它总是从第二个输入开始

无法在循环中输入第一个字符串,在java中它总是从第二个输入开始,java,arrays,class,for-loop,oop,Java,Arrays,Class,For Loop,Oop,我创建了一个Employee类,在该类中我只传递值并打印数组。你可以看到: package practicequestion; import java.util.Scanner; import practicequestion.employeeq.Employee; public class EmployeeMain { public static void main(String[] args) { Employee[] obj = new Employee[3];

我创建了一个Employee类,在该类中我只传递值并打印数组。你可以看到:

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

}

有人能解决这个问题吗?

这里已经回答了这个问题:

基本上,问题在于Scanner.nextInt()在进入循环之前调用它。 Scanner.nextLine()只读取用户输入的下一个整数,而不是整行,因此当您在直接接受输入整数的行的剩余部分(和空部分)后调用Scanner.nextLine()时


要解决此问题,您必须调用Scanner.nextLine()一次,或者使用Scanner.nextLine()获取整数,然后将其强制转换为整数。

尝试此方法。我将记录我在哪里做的更改

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

Scanner sc=新扫描仪(System.in);
System.out.println(“输入所需的输入数量”);
int n=sc.nextInt();
//在获得输入数后分配Employee数组。
员工[]obj=新员工[n];
对于(int i=0;i

关闭扫描仪通常不是一个好的做法。

谢谢你的建议。现在我也可以输入,但无法打印名字。请指导我怎么做?
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();
}