string array loop print value null java -


i have instantiated string array containing 10 strings. want ask user enter subject name until 10 strings finished, or if user enters "q" quit. once happens, string array elements should printed via printarray method. have far, "null" value displayed each value after "the array elements:" make total of 10 strings. happens if enter "q" after few entries , not ten. i'd rid of "null" values , if user doesn't enter "q", after 10th entry, should display 10 arrays.

{     // instantiate string array can contain 10 items.     string[] array = new string[10];      // read names of subjects array     // , count how many have been read in.     // there may fewer 10.     scanner input = new scanner(system.in);      system.out.println("please enter subject name or enter q quit: ");     string subject = input.nextline();     int i=0;     while (!"q".equals(subject))     {         array[i]=subject;         i++;           system.out.println("please enter subject name or enter q quit: ");         subject = input.nextline();     }     input.close();     system.out.println("the array elements:");        // call printarray print names in array.     printarray(array); }   /**  * method printarray prints string values   * in partially-filled array, 1 per line.    * significant items in array should printed.  */ public static void printarray(string[] args) {      for(string val : args)         system.out.println(val); } 

 string quit = "q"; 

and

while (!"q".equals(quit)) 

this same as

 while(!true) 

so never go inside while loop

i think must check

 while (!subject.equals(quit)) 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -