I have this problem. Suppose I want to take user input starting from the first line of input as integer that will determine the number of inputs, and the inputs must be a numbers of 4 categories (day , hr, min, sec). As an example of input
Input:
Enter integer: 3
4 5 6 3555
6 11 3 120
1 0 44 11
Now what I want to do with these inputs is store them and seperate each line containing the 4 numbers (then seperate the 4 numbers) and then I want to pass each line into a method that accepts 4 parameters.
Is it possible? And how to code it?
Advertisement
Answer
You can use BufferedReader with InputStreamReader and read each line as String
and wrap it using Wrapper class to int for converting String to int we use this method Integer.parseInt(<String yourString>)
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int totalLines = Integer.parseInt(br.readLine());
for(int i = 0; i < totalLines ; i++){
String [] num = br.readLine().split(" ");
int num1 = Integer.parseInt(num[0]);
int num2 = Integer.parseInt(num[1]);
int num3 = Integer.parseInt(num[2]);
int num4 = Integer.parseInt(num[3]);
// here call the method you want to call
}
.split(" ")
will split the line on the base of the parameter we define in it, in your case it was space
and remember split returns a String array