user3385542
user3385542

Reputation: 59

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

Upvotes: 0

Views: 69

Answers (2)

Sri777
Sri777

Reputation: 540

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

Upvotes: 0

Velox
Velox

Reputation: 254

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

Upvotes: 1

Related Questions