why this loop is running only two times while taking input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why this loop is running only two times while taking input

import java.util.*; class Reverse { public static void main(String[] arr) { Scanner sc=new Scanner(System.in); int n=3; String[] lin=new String[n]; for(int i=0;i<3;i++){ lin[i]=sc.nextLine(); } for(int i=0;i<3;i++){ String[] revl=lin[i].split(" "); for(int j=revl.length-1;j>=0;j--) { System.out.print(revl[j]+" "); } System.out.println(); } } }

7th Oct 2016, 2:27 PM
Aamir Alam
Aamir Alam - avatar
3 Answers
+ 2
Both of your for statements are increasing i. Try changing one of the i variables then it should run 9 times
8th Oct 2016, 3:20 AM
Parker L
Parker L - avatar
+ 2
put a different counter variable in for your second for loop
8th Oct 2016, 8:31 PM
Michael McGaha
Michael McGaha - avatar
0
thanks
21st Oct 2016, 12:55 PM
Aamir Alam
Aamir Alam - avatar