0
 import java.util.Scanner;
    public class stringclass {
        public static void main(String [] args){
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the number of friends: ");
            int a = input.nextInt();
            String [] friends = new String[a];
            int count=1;

            for(int i=0;i<a;i++){
                System.out.println("Enter the name of your friend "+count +":");
                friends[i] = input.nextLine();
                count++;            
            }
            int count1=1;
            for (int j=0;j<a;j++){
                System.out.println("Name of your friend "+count1+ "is "+friends[j].toUpperCase());
                count1++;
            }
        }
    }

I was writing code to display name of friends using string in java by getting input. But index 0 is not getting a entry

Nicktar
  • 5,288
  • 1
  • 24
  • 40
  • google nextLine() after nextInt(). This is a very common question – Stultuske Dec 10 '19 at 13:09
  • Add `input.nextLine();` after `int a = input.nextInt();` in order to consume the end of the line that contains the input integer. – Eran Dec 10 '19 at 13:09
  • 2
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Ashutosh Sharma Dec 10 '19 at 13:20

2 Answers2

0
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of friends: ");
    int a = Integer.parseInt(input.nextLine());
    String[] friends = new String[a];
    for (int i = 0; i < a; i++) {
        System.out.print("Enter the name of your friend " + (i + 1) + ":");
        friends[i] = input.nextLine();
    }
    for (int j = 0; j < a; j++) {
        System.out.println("Name of your friend " + (j + 1) + "is " + friends[j].toUpperCase());
    }

}
  • it would be better if you explained what you changed, and why you changed it. Just posting code that is ready to copy-paste is not really a very good answering technique – Stultuske Dec 10 '19 at 13:47
0

You need to consume the end of the line that contains the input integer, as explained in comments.

import java.util.Scanner;
        public class stringclass {
            public static void main(String [] args){
                Scanner input = new Scanner(System.in);
                System.out.print("Enter the number of friends: ");
                int a = input.nextInt();
                // This is what you want
                input.nextLine();
                String [] friends = new String[a];
                int count=1;

                for(int i=0;i<a;i++){
                    System.out.println("Enter the name of your friend "+count +":");
                    friends[i] = input.nextLine();
                    count++;            
                }
                int count1=1;
                for (int j=0;j<a;j++){
                    System.out.println("Name of your friend "+count1+ "is "+friends[j].toUpperCase());
                    count1++;
                }
            }
        }
Sachin Sachdeva
  • 10,526
  • 37
  • 100