问题描述:

public static public static void main(String[] args) {

System.out.print("Enter the number of student names to input: ");

int studentNum = new Scanner(System.in).nextInt();

String students[ ] = new String[studentNum];

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

students[ i ] = inputStudent(i+1);

}

}

public static String[] inputStudent(int i) {

String studentNum[] = new String[i];

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

System.out.print("Enter student name"+(a+1)+": ");

studentNum[a]=new Scanner(System.in).nextLine();

}

return studentNum;

}

ERROR:

students[ i ] = inputStudent(i+1);

IT SAYS:

incompatible types: String[] cannot be converted to String

PS:

The main function should not be modified.

网友答案:

You are trying to assign String array to String

i.e

inputStudent(int i) returns Array, but you are trying to assign Array to students[ i ] = inputStudent(i+1); As String[i] is the element String array which will accept String only

网友答案:

Change your method to this. You cannot assign a String array to a String. Also, why do you loop in your method? If I understand correctly you want to add n students to a student array? You loop in your main and method runs for each student.

public static String inputStudent(int i) {
        System.out.print("Enter student name");
        String studentName = new Scanner(System.in).nextLine();
        return studentName;
    }
网友答案:

Modify your input student method in such a way that it returns a single student only.

public static String inputStudent(int i) {
    String studentNam = null;
    System.out.print("Enter student name"+i+": ");
    studentNum=new Scanner(System.in).nextLine();
    return studentNum;
}
网友答案:

Since you stipulate that the main function can not be modified you are going to have to change the return type of your inputStudent function from String[] to String. This will mean you have to change how you are storing the data inside that function.

So, change the String studentNum[] to String studentNum and instead of asking for a new user input for every index of the array, ask for all inputs at once comma separated and store them in studentNum.

This is one possible approach according to your criteria.

网友答案:

I understand what you want to implement, the code below will solve your problem, every line of code has the explanation.

public static void main(String[] args) throws Exception {
    System.out.print("Enter the number of student names to input: ");
    Scanner scanner = new Scanner(System.in); // get the scanner
    int numOfStudents = scanner.nextInt();    // get the number of students from user
    String [] students = new String [numOfStudents];    // create an array of string
    int studentAdded = 0;   // counter
    scanner = new Scanner(System.in); // recreate the scanner, if not it will skip the first student.
    do {
        System.out.println("Enter student name "+(studentAdded+1)+": ");
        String studentName = scanner.nextLine();    // ask for student name
        students[studentAdded] = studentName;       // add student name to array
        studentAdded++;     // add 1 to the counter
    } while (studentAdded < numOfStudents); // loop until the counter become the same size as students number
    System.out.println("students = " + Arrays.toString(students)); // and show the result
}
网友答案:

What you can do to do is change:

students[ i ] = inputStudent(i+1);

to

students[ i ] = Arrays.toString(inputStudent(i + 1));

As students is a String you are unable to assign an array directly to a String. By using Array.toString() it will return a representation of the contents of the array as a String value.

Looking at your question again you are unable to modify the main. In this case we can set inputStudent to return a String type.

public static String inputStudent( int i )
{
    String studentNum[] = new String[i];
    for( int a = 0; a < i; a++ )
    {
        System.out.print( "Enter student name" + (a + 1) + ": " );
        studentNum[a] = new Scanner( System.in ).nextLine();
    }
    return Arrays.toString(studentNum);
}

Or short and sweet.

public static String inputStudent( final int id )
{
    System.out.print("Enter student name at ID: "+id+": ");
    return new Scanner( System.in ).next();
}
网友答案:

You have an error in your code. For loop is not needed in main

public static public static void main(String[] args) {
    System.out.print("Enter the number of student names to input: ");
    int studentNum = new Scanner(System.in).nextInt();
    String students[ ] = inputStudent(studentNum);
}

public static String[] inputStudent(int i) {
    String studentNum[] = new String[i];
    for(int a=0; a<i; a++) {
        System.out.print("Enter student name"+(a+1)+": ");
        studentNum[a]=new Scanner(System.in).nextLine();
    }
    return studentNum;
}
相关阅读:
Top