1

I'm trying to initialize an array of string with length of n. But when I run the program it takes (n-1) input and prints (n-1) results as well. Here is my code...

import java.io.*;
import java.util.*;

public class string_array{
public static void main(String[] args){
    int i;

    Scanner sc=new Scanner(System.in);
    System.out.println("Enter no. of participants(cannot be 0):");
    int n=sc.nextInt();
    if(n>0){
        String[] participants=new String[n];
        System.out.println("Total no. of participants: "+n);
        System.out.println("Enter name of participants:");

        for( i=0;i<participants.length;i++){

            participants[i]=sc.nextLine();
        }
        System.out.println("Name of participants: ");
        for( i=0;i<participants.length;i++){
            System.out.println(participants[i]);
        }
    }
    else{
        System.out.println("Invalid Input.");
    }
}
}
Jyotirmoy S.
  • 101
  • 1
  • 2

0 Answers0