0

For example, I entered a size of 3 Students. It skips index 0 in the console also in printing.

Please refer to this image, I have a sample size of 3 students and its output.

I don't have the slightest idea of why it skips index 0? Thanks for the help!

import java.util.Arrays;
import java.util.Scanner;

class string{

    public static void main(String [] args){
        Scanner console = new Scanner(System.in);
        
        System.out.print("Enter Student Size: ");
        int studentSize = console.nextInt();
        String [] arrName = new String[studentSize];

        for (int i=0; i<arrName.length; i++){
            System.out.print("Enter student name: ");
            String nameString = console.nextLine();
            arrName[i] = nameString;
        }

        System.out.print(Arrays.toString(arrName));

        //Closing Braces for Class and Main
    }
}

2 Answers 2

1

The problem is with the console.nextInt(), this function only reads the int value.So In your code inside the loop console.nextLine() first time skip the getting input.just puting console.nextLine()afterconsole.nextInt() you can solve the problem.

public static void main(String [] args){
        Scanner console = new Scanner(System.in);

        System.out.print("Enter Student Size: ");
        int studentSize = console.nextInt();
        console.nextLine();
        String [] arrName = new String[studentSize];

        for (int i=0; i<arrName.length; i++){
            System.out.print("Enter student name: ");
            String nameString = console.nextLine();
            arrName[i] = nameString;
        }

        System.out.print(Arrays.toString(arrName));

        //Closing Braces for Class and Main

    }
Sign up to request clarification or add additional context in comments.

Comments

0

The reason for this skip is due to the different behaviour of the console.nextInt() and console.nextLine() as:

console.nextInt() reads the integer value entered, regardless of whether you hit the enter for new-line.

console.nextLine() reads the whole line, but as you previously hit enter when you give the size of Array.

3 was accepted as the size of the Array and when you hit enter it was accepted as the first value for your array which is a blank space or I can say it is referred to as "".

Following are the two resolutions for this:

  1. Either put a console.nextLine() call after each console.nextInt() to consume rest of that line including newline

  2. Or, even better, read the input through Scanner.nextLine and convert your input to the proper format you need. you may convert to an integer using int studentSize = Integer.parseInt(console.nextLine()) method. (Surround it with try-catch)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.