1
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.

1
  • 1
    Can you explain the purpose of inputStudent function. It must not return array of students, it should read a single student and just return it. Commented Feb 27, 2016 at 14:26

7 Answers 7

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

5 Comments

Do you really need to read 'n' number of students in this function. Why So?
If he can't modify the main then he can't return a String[] from inputStudent, so it has to be changed.
Still main method will not be modified with this way. I guess he puts the wrong logic in either of the method. Only one method should have a for loop inside.
I agree Python...was confirming your answer.
And the point that he can't return 'n' number of students in that method for it to work without modifying main :)
0

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.

Comments

0

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
}

Comments

0

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

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.