filling array gradually with data from user

Posted by neville on Stack Overflow See other posts from Stack Overflow or by neville
Published on 2010-05-15T00:26:26Z Indexed on 2010/05/15 0:34 UTC
Read the original article Hit count: 474

Filed under:
|
|
|
|

I'm trying to fill an array with words inputted by user. Each word must be one letter longer than previous and one letter shorter than next one. Their length is equal to table row index, counting from 2. Words will finally create a one sided pyramid, like :
A
AB
ABC
ABCD

Scanner sc = new Scanner(System.in);
System.out.println("Give the height of array: ");
String[] words = new String[height];
for(int i=2; i<height+2; i++){
    System.out.println("Give word with "+i+" letters.");
    words[i-2] = sc.next();
    while( words[i-2].length()>i-2 || words[i-2].length()<words[i-3].length() ){
        words[i-2] = sc.next();
    }
}

Currently the while loop doesn't influence scanner at all :/

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays