-2

I have instantiated a String array containing 10 strings. I basically want to ask the user to enter a subject name until all 10 strings are finished, or if the user enters "q" to quit. Once this happens, the String array elements should be printed via the printArray method. This is what I have so far, but I get a "null" value displayed for each value after "The Array Elements:" to make up a total of 10 strings. This happens if I enter "q" after a few entries and not all ten. I'd like to get rid of the "null" values and also if the user doesn't enter "q", after the 10th entry, then it should display the 10 arrays.

{
    // Instantiate a String array that can contain 10 items.
    String[] array = new String[10];

    // Read names of subjects into this array
    // and count how many have been read in.
    // There may be fewer than 10.
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter a subject name or enter q to quit: ");
    String subject = input.nextLine();
    int i=0;
    while (!"q".equals(subject))
    {
        array[i]=subject;
        i++;


        System.out.println("Please enter a subject name or enter q to quit: ");
        subject = input.nextLine();
    }
    input.close();
    System.out.println("The Array Elements:");



    // Call printArray to print the names in the array.
    printArray(array);
}


/**
 * Method printArray prints the String values 
 * in a partially-filled array, one per line.  Only the 
 * significant items in the array should be printed.
 */
public static void printArray(String[] args)
{

    for(String val : args)
        System.out.println(val);
}
Bob
  • 39
  • 1
  • 8
  • _but I believe that the loop is not working properly_ Why? – Sotirios Delimanolis Sep 19 '14 at 04:53
  • 1
    It is a good time to learn how to use a debugger: debug your code step by step and check the values of the `quit` and `subject` and `input` variables as you move on. You will probably find the problem. – assylias Sep 19 '14 at 04:54
  • Your while loop isn't even starting, since you're essentially saying `while "q" is not equal to "q"` – Krease Sep 19 '14 at 04:54
  • Once you fix the "q"=quit problem, you will probably want to add something to your while loop to exit after 10 entries have been received. – lone gold Sep 19 '14 at 04:59

3 Answers3

1
    String quit = "q";
....
    while (!"q".equals(quit))

What do you expect? quit will always be equal to "q", so the loop is never entered.

You probably need while (!"q".equals(subject)), since subject is the variable you change in the loop.

Eran
  • 387,369
  • 54
  • 702
  • 768
1
 String quit = "q";

and

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

this is same as

 while(!true)

so you will never go inside to the while loop

I think you must check

 while (!subject.equals(quit))
0
for(i=0i<10;i++)
{
    System.out.println(array[i]);
}

just put this where you calling function. don't call another method to show the array.

Aiyaz Parmar
  • 562
  • 6
  • 17