Input in java using Buffer Reader
Example: Multiple line array reader
Input: 5 1 2 3 4 5 Output: [1, 2, 3, 4, 5]
Approach
Java: Using BufferReader
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class BufferedReaderInput {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(br.readLine());int n = Integer.parseInt(st.nextToken());int arr[] = new int[n];for (int i = 0; i < n; i++) {int x = Integer.parseInt(br.readLine());arr[i] = x;}System.out.println(Arrays.toString(arr));}}
Example: Single line array reader
Input: 5 1 2 3 4 5 Output: [1, 2, 3, 4, 5]
Approach
Java: Using BufferReader
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;public class BufferedReaderInput {public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StringTokenizer st = new StringTokenizer(br.readLine());int n = Integer.parseInt(st.nextToken());int arr[] = new int[n];st = new StringTokenizer(br.readLine());for (int i = 0; i < n; i++) {int x = Integer.parseInt(st.nextToken(" "));arr[i] = x;}System.out.println(Arrays.toString(arr));}}
No comments:
Post a Comment