If I am using the code in the comments it's not filling the requirments.
but with buffered reader it is working
What am I missing from the code in the comment?
Code in the comment!
Thank in advance :)
what is the difference between the two code?
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
RKVengatesh Enterprise Java Developer at Fragma Data Systems
6 October 2019, 01:27
simply add sc.nextLine(); before the "list.add(sc.nextLine()) line"....it will work
0
Adnan Razzaq
24 September 2019, 21:46
In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine() doesn’t not read values from console and cursor will not come into console it will skip that step. The nextXXX() methods are nextInt(), nextFloat(), nextByte(), nextShort(), nextDouble(), nextLong(), next()
https://www.geeksforgeeks.org/difference-between-scanner-and-bufferreader-class-in-java/
+1
Gábor
21 September 2019, 21:03
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
int n =Integer.parseInt(reader.readLine()); //int n = sc.nextInt();
int m =Integer.parseInt(reader.readLine()); //int m = sc.nextInt();
for(int i = 0; i<n; i++){
list.add(reader.readLine()); //list.add(sc.nextLine());
}
for (int i=0; i<m; i++){
list.add(list.get(0));
list.remove(0);
}
for (int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
0