Results 1 to 5 of 5
  1. #1
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Question Input skips on array

    Good day!

    I was wondering why my input skips on the second loop on my array. I'm really confused since my code looks good at first loop. Hope somebody can help me on this matter... THanks in advance.. Pls view my attached file... thanks :)
    Attached Files Attached Files

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Hi kryptonite03, welcome.

    It's better to actually post the code so everyone can get at it easily. There's a code button you can use for that. Or put [CODE] at the start of your code and [/CODE] at the end.

    I'll post it here so I can read it...

    Java Code:
    import java.util.Scanner;
    public class ArrraySample {
            
       
        public static void main(String[] args) {
            String[] lastname = new String [10];
            String[] firstname = new String [10];
            int []age = new int [10];
            Scanner input = new Scanner (System.in);
            int n = 0;
            for (n = 0; n < 3; n++)
            {
                System.out.println("Input no" + (n + 1));
                System.out.println("Enter last name");
                lastname[n] = input.nextLine();
                System.out.println("Enter first name");
                firstname[n] = input.nextLine();
                System.out.println("Enter age");
                age[n] = input.nextInt();
            
            }
            for (n = 0; n < 3; n++)
            {
                System.out.println("Lastname \t\t Firstname \t\t Age"  );
                System.out.println(lastname[n] + "\t\t" + firstname[n] + "\t\t" + age[n]);
            
            }
        }
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    age[n] = input.nextInt();

    This line could cause problems because nextInt() reads (and returns) an integer from the input stream. But it leaves the current position in the stream just before the newline. What this means is that next time you call nextLine() you will get an empty string returned.

    One way to avoid this is to call nextLine() immediately after nextInt() and do nothing with the empty string it returns.

    Java Code:
    for (n = 0; n < 3; n++)
    {
        System.out.println("Input no" + (n + 1));
        System.out.println("Enter last name");
        lastname[n] = input.nextLine();
        System.out.println("Enter first name");
        firstname[n] = input.nextLine();
        System.out.println("Enter age");
        age[n] = input.nextInt();
        [b]input.nextLine();[/b]
    }

  4. #4
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Good day! My apologies for not posting the codes...

    pbrockway2, Thank you very much for guiding me.. Im really new to Java.. The input.nextLine(); statment works well!

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. java skips a block of code...
    By JavaSal in forum New To Java
    Replies: 8
    Last Post: 02-07-2010, 09:58 AM
  2. Array Input
    By Rose88 in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 10:39 PM
  3. while loop skips code
    By ejs7597 in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 03:07 AM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 07:29 AM
  5. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 12:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •