Why is my program accepting and printing 1 less inputs than the entered length? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is my program accepting and printing 1 less inputs than the entered length?

import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String ar[]=new String[n]; int a=1; for(int i=0;i<n;i++){ ar[i]=sc.nextLine(); } for(int i=0;i<n;i++){ System.out.println(ar[i]); } } }

19th Sep 2023, 7:15 PM
Om Verma
Om Verma - avatar
1 Answer
+ 4
Try it with n +=1 and "i <= n;".
19th Sep 2023, 7:50 PM
Ausgrindtube
Ausgrindtube - avatar