Help with String array program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfshake
    New Member
    • Nov 2007
    • 16

    #1

    Help with String array program

    I am try to make the user enter 10 names and then display them out in
    reverse order. I am having trouble with my for statement. can someone help me. Thanks


    [CODE=java] Scanner reader = new Scanner (System.in);
    String [] names = new String [10];
    int count = 0;
    while(count < names.length){
    System.out.prin t("Enter your name: ");
    String name = reader.nextLine ();
    names[count] = name;
    count++;
    }
    System.out.prin t("\nHere are the names:");
    for(int i = 0; i < count; i++){
    System.out.prin t("\n" + names[i] + "\n");
    }[/CODE]
    Last edited by Ganon11; Jan 29 '08, 01:31 AM. Reason: Please use the [CODE] tags provided.
  • eyeofsoul
    New Member
    • Sep 2007
    • 31

    #2
    the problem is with your for loop..instead start from 0, start from 9. the string is stored in array.

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      [QUOTE=mfshake]I am try to make the user enter 10 names and then display them out in
      reverse order. I am having trouble with my for statement. can someone help me. Thanks

      [CODE=java] Scanner reader = new Scanner (System.in);
      String [] names = new String [10];

      Also new String [10]; would give you 11 names not 10


      nomad

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by nomad
        Also new String [10]; would give you 11 names not 10
        That's wrong. maybe you are thinking of VB, but that will create an array of length 10, with indices [0] up to and including [9].

        Comment

        • mfshake
          New Member
          • Nov 2007
          • 16

          #5
          Help with array

          Originally posted by eyeofsoul
          the problem is with your for loop..instead start from 0, start from 9. the string is stored in array.
          it says i have a negative value after my progam finishes
          Last edited by mfshake; Jan 29 '08, 09:20 PM. Reason: fixed my code

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by mfshake
            it says i have a negative value after my progam finishes
            It's hard to comment on code we can't see (hint).

            Comment

            • mfshake
              New Member
              • Nov 2007
              • 16

              #7
              Originally posted by BigDaddyLH
              It's hard to comment on code we can't see (hint).
              [CODE=Java]
              Scanner reader = new Scanner (System.in);
              String [] names = new String [10];
              int count = 0;

              while(count < names.length){
              System.out.prin t("Enter your name: ");
              String name = reader.nextLine ();
              names[count] = name;
              count++;
              }//close while

              System.out.prin t("\nHere are the names:");
              for(int i = 9; i < count; i--){
              System.out.prin t("\n" + names[i] + "\n");
              }//close for
              [/CODE]

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Remember a loop terminates when its test condition is false.

                Comment

                • Raghunandan24
                  New Member
                  • Dec 2007
                  • 31

                  #9
                  Originally posted by BigDaddyLH
                  Remember a loop terminates when its test condition is false.
                  The best solution would be to look at each line of code and check the value of each variable and BigDaddy's hint!

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by mfshake
                    [CODE=Java]
                    Scanner reader = new Scanner (System.in);
                    String [] names = new String [10];
                    int count = 0;

                    while(count < names.length){
                    System.out.prin t("Enter your name: ");
                    String name = reader.nextLine ();
                    names[count] = name;
                    count++;
                    }//close while

                    System.out.prin t("\nHere are the names:");
                    for(int i = 9; i < count; i--){
                    System.out.prin t("\n" + names[i] + "\n");
                    }//close for
                    [/CODE]
                    Better go through your loop "manually" writing down the values of i and count at each pass. That way you can be able to see what is going on (or what is not going on).

                    Comment

                    Working...