javaarraysinputjava.util.scannerincompatibletypeerror

Incompatible types when trying to input data into an array


I'm having problems with inputting data interactively into arrays. I'm trying to use the nextLine method to add a set of 12 names into the array, but when I compile at the end of line 12 it gives me the error "Incompatible Types".

public class nextLineArray {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char names[] = new char[12];
        System.out.println("Enter the 12 names: ");

        for(int i = 0; i < 12; i++) {
            names[i] = input.nextLine();
        }

        System.out.println(names);
    }
}

Solution

  • This is because the Scanner.nextLine() returns a String, not a char

    Try changing

    char names[]=new char[12];
    

    To

    String names[] = new String[12];