Q:

Copy a string of words to an array in Java

Ive been writing a program that is able to calculate a person's grades, but Im unable to turn a string into an String array (it says the array length is 1 when i put in 3 words). Below is my code. Where am I going wrong??
    protected static String[] getParts(){
    Scanner keyboard = new Scanner(System.in);

    System.out.println( "What assignments make up your overall grade? (Homework, quizzes, etc)" );
    String parts = keyboard.next();     

    Pattern pattern = Pattern.compile(" ");
    String[] assignments = pattern.split(parts);

    // to check the length

    for ( int i = 0; i < assignments.length; i++ )
        System.out.println(assignments[i]);

    return assignments;

}

A:

Scanner::nextLine
Scanner::next() only consumes one word (it stops at whitespace). You want Scanner::nextLine, which consumes everything until the next line, and will pick up all your words.

