Member Avatar for ragnacrap

I got a problem in a program I'm working on here:

        String[ ][ ] applicant = new String[appno][2];

        for (i = 0; i < applicant.length; i++){
            System.out.println("Input applicant's name: ");
            appname = keyboard.nextLine();
            applicant[i][0] = appname;
            System.out.println("Input applicant's position: ");
            apppos = keyboard.nextLine();
            applicant[i][1] = apppos;
        }

The input works, but the code seems to skip the first instance of "appname = keyboard.nextLine()" every time (all further instances allow me to input). How do I solve this?

Recommended Answers

All 2 Replies

There may be some data in the keyboard buffer that has not been consumed when the loop is called. You might try flushing the input buffer first. What version of Java are you using? Also, what is the actual class you are using for keyboard input. There is no keyboard class per-se in current Java implementations as far as I know.

Print out the contents of appname after it is read into to see if an empty String has been read. Be sure to put delimiters around appname when you print it so you can see if it is empty:
(">"+appname+"<").

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.