Question

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);
    }
}
Était-ce utile?

La 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];

Autres conseils

Why do you use Char for storing names?? Use Strings instead.

And also nextLine() returns a String not a char. So the error.

Just FYI... you can even use next() to get the input from console if you doesnt want the input to be null or empty. nextLine() takes even an empty string as input. Try next()

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top