Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: nested array loop

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default nested array loop

    okay so i have this nested loop for an array.
    when i run it
    so i input the first name then the 5 scores but after the 5 scores it skips the second name and goes to the 5 scores again.
    I have a picture below when i run it.

    import java.util.Scanner;
     
    public class test
    {
       public static void main(String[] args)
       {
       String[] names = new String[5];
       int[][] scores = new int[5][5];
     
       String name ="";
     
       Scanner keyboard = new Scanner(System.in);
     
       for (int index=0;index<=4;index++)
       {
          System.out.println("enter name");
          name= keyboard.nextLine();
          name += names[index];
     
          for (int index2 = 0; index2<=4;index2++)
       {
          System.out.println("enter test score");
          scores[index][index2] = keyboard.nextInt();
     
       }
       }
     
     
       }
       }

    help.jpg


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    It will be the scanner bug (not really a bug). You can google it.

    After you read a number clear the buffer with;
    keyboard.next ();

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nested array loop

    i dont get it

    do i put
     System.out.println("enter test score");
          scores[index][index2] = keyboard.nextInt();
         keyboard.next (); //<<<< like this??

  4. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Try it

    --- Update ---

    Okay, I'm at a computer now and not reading off my phone.

    There are two things wrong.

    name += names[index];

    This is the wrong way to assign the input to the names array.

    Also, I have given you a slight bum steer on clearing the buffer. Use;
    keyboard.nextLine();

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nested array loop

    sorry i dont really get what you are trying to say. im new to java programming sorry.

    like this?

     for (int index=0;index<=4;index++)
       {
          System.out.println("enter name");
          names[index]= keyboard.nextLine();
     
     
          for (int index2 = 0; index2<=4;index2++)
       {
          System.out.println("enter test score");
          scores[index][index2] = keyboard.nextInt();
     
     
       }

  6. #6
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: nested array loop

    Don't apologise, being new is not something to be sorry for. Ask direct questions if you don't understand something. Did you google the 'Scanner Bug'

    That new piece of code will assign the name correctly, now you need to clear the scanner buffer either after you've read all the scores or after you read each score using;
    keyboard.nextLine();

  7. The Following User Says Thank You to Kewish For This Useful Post:

    jbenz (November 7th, 2013)

  8. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: nested array loop

    GOT it thank you so much!

  9. #8
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: nested array loop

    You're welcome. Great work!

    Some Tips;

    Class files always start with a capital letter.

    Rather than set the value for the array size, could you maybe rewrite it so that you use either constant values such as
    final int NUM_OF_PEOPLE = 5;
    final int NUM_OF_SCORES = 5;
    or you could use user inputted values or a combination of both.

    How would you use those to create the array objects?

    In your for loops, did you know that you can use the array length as an upper limit for the counter?
    index < scores.length; // for outer loop
    index2 < scores[index].length; // for the inner loop
    That way if your array size changes say by user input, your code will still function. The user could say they wanted to put in 100 people with 3 scores each and the loop will still work providing you create the arrays with those values.

    Anyway, enough of that. Good luck with Java.

Similar Threads

  1. nested for loop
    By tardis_ in forum Loops & Control Statements
    Replies: 4
    Last Post: April 18th, 2013, 01:11 PM
  2. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum Loops & Control Statements
    Replies: 1
    Last Post: November 24th, 2012, 10:43 AM
  3. Please Help: Need Help with my Nested Loop
    By hiroprotagonist in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 20th, 2012, 03:49 PM
  4. Nested For Loop!
    By samadniz in forum Object Oriented Programming
    Replies: 3
    Last Post: September 3rd, 2012, 04:03 PM
  5. Nested for loop
    By wolf_fcknHaley33 in forum Loops & Control Statements
    Replies: 2
    Last Post: May 23rd, 2012, 08:49 AM